小编Pho*_*nix的帖子

UICollectionView图像闪烁

我有一个UICollectionView,可以全屏显示单元格(即图库).有时,当扫描新图像时,除了右图像之外的图像显示的时间不到一秒,而是更新图像.其他时候,会显示错误的图像,但如果您向左和向右滑动(即图像消失并重新出现),则会出现右图像.我不明白这种行为的原因.当集合视图需要时,可以异步下载图像.这是代码:

 let blank = UIImage(named: "blank.png")
 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell

    let image = images[indexPath.row]
    if let imageView = cell.viewWithTag(5) as? UIImageView {
        imageView.image = blank
        if let cachedImage = cache.objectForKey(image.url) as? UIImage {
            imageView.image = cachedImage
        } else {
            UIImage.asyncDownloadImageWithUrl(image.url, completionBlock: { (succeded, dimage) -> Void in
                if succeded {
                    self.cache.setObject(dimage!, forKey: image.url)
                    imageView.image = dimage
                } 
            })
        }
    }

    return cell
}
Run Code Online (Sandbox Code Playgroud)

其中UIImage.asyncDownloadImageWithUrl是:

extension UIImage { …
Run Code Online (Sandbox Code Playgroud)

uicollectionview swift

2
推荐指数
1
解决办法
3695
查看次数

标签 统计

swift ×1

uicollectionview ×1