Swift 2 中缓存图像的粘性滚动
Sticky scroll from cached images in Swift 2
我使用此代码在 UICollectionView 中显示一些图像,但可以说单元格是 25。当我向下移动到最后一个单元格然后我尝试向上移动到第一个时,图像再次加载(来自缓存,但它仍然显示加载指示器。
我的 phone 是 iPhone SE,我没有很多应用程序,phone 是 运行 完美。如果我将我的应用程序加载到上面有很多数据的较旧的 iphone,那么滚动会变得很粘而且非常烦人。我们如何避免在索引路径中项目的集合视图函数单元格内发生这种情况???
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("RecipesCell", forIndexPath: indexPath) as! RecipesCell
// Get Cover image
let myCache = ImageCache(name: recipesClass.objectId!)
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
let optionInfo: KingfisherOptionsInfo = [
.DownloadPriority(0.5),
.CallbackDispatchQueue(queue),
.Transition(ImageTransition.Fade(1)),
.TargetCache(myCache)
]
if let imageFile = recipesClass[RECIPES_COVER] as? PFFile {
let URL = NSURL(string: imageFile.url!)!
cell.coverImage.kf_setImageWithURL(URL, placeholderImage: nil,
optionsInfo: optionInfo,
progressBlock: { receivedSize, totalSize in
print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
},
completionHandler: { image, error, cacheType, imageURL in
print("\(indexPath.row + 1): Finished")
print("cacheType: \(cacheType) \(indexPath.row + 1): Finished")
})
} else {
cell.coverImage.image = UIImage(named:"logo")
}
有人遇到同样的问题吗?
谢谢!
仅供参考,我正在使用 parse 和 kingfisher 进行缓存
经过几天关于 UI 套件的研究并使用 Instruments > Time Profiler 分析我的应用程序后,我发现 image decoding
正在主线程上执行。这是不好的做法。所以 Kingfisher
有一个选项可以解码背景上的图像,我的应用程序在旧 iPhone 上运行得更快,但仍然有点粘。所以我取消了单元格上的圆角并启用了分页,这样它会减慢滚动速度,现在很棒。
对于背景图像解码,我使用了这个
let optionInfo: KingfisherOptionsInfo = [
.DownloadPriority(0.5),
.CallbackDispatchQueue(queue),
.Transition(ImageTransition.Fade(1)),
.TargetCache(myCache),
.BackgroundDecode //this is the new option I added
]
我使用此代码在 UICollectionView 中显示一些图像,但可以说单元格是 25。当我向下移动到最后一个单元格然后我尝试向上移动到第一个时,图像再次加载(来自缓存,但它仍然显示加载指示器。 我的 phone 是 iPhone SE,我没有很多应用程序,phone 是 运行 完美。如果我将我的应用程序加载到上面有很多数据的较旧的 iphone,那么滚动会变得很粘而且非常烦人。我们如何避免在索引路径中项目的集合视图函数单元格内发生这种情况???
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("RecipesCell", forIndexPath: indexPath) as! RecipesCell
// Get Cover image
let myCache = ImageCache(name: recipesClass.objectId!)
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
let optionInfo: KingfisherOptionsInfo = [
.DownloadPriority(0.5),
.CallbackDispatchQueue(queue),
.Transition(ImageTransition.Fade(1)),
.TargetCache(myCache)
]
if let imageFile = recipesClass[RECIPES_COVER] as? PFFile {
let URL = NSURL(string: imageFile.url!)!
cell.coverImage.kf_setImageWithURL(URL, placeholderImage: nil,
optionsInfo: optionInfo,
progressBlock: { receivedSize, totalSize in
print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
},
completionHandler: { image, error, cacheType, imageURL in
print("\(indexPath.row + 1): Finished")
print("cacheType: \(cacheType) \(indexPath.row + 1): Finished")
})
} else {
cell.coverImage.image = UIImage(named:"logo")
}
有人遇到同样的问题吗? 谢谢!
仅供参考,我正在使用 parse 和 kingfisher 进行缓存
经过几天关于 UI 套件的研究并使用 Instruments > Time Profiler 分析我的应用程序后,我发现 image decoding
正在主线程上执行。这是不好的做法。所以 Kingfisher
有一个选项可以解码背景上的图像,我的应用程序在旧 iPhone 上运行得更快,但仍然有点粘。所以我取消了单元格上的圆角并启用了分页,这样它会减慢滚动速度,现在很棒。
对于背景图像解码,我使用了这个
let optionInfo: KingfisherOptionsInfo = [
.DownloadPriority(0.5),
.CallbackDispatchQueue(queue),
.Transition(ImageTransition.Fade(1)),
.TargetCache(myCache),
.BackgroundDecode //this is the new option I added
]