【发布时间】:2017-03-29 08:31:10
【问题描述】:
我正在使用 UICollectionView 在我的应用中显示图像。
问题是显示图像的速度非常慢。 50 秒后,将出现集合视图中的图像。 :(
当我在谷歌找到解决方案时,他们大多编写以下代码。
但这对我来说行不通。
cell.layer.shouldRasterize = true
cell.layer.rasterizationScale = UIScreen.main.scale
和
extension SeeAllCollectionView {
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
debugPrint("seeAllLIStCell Count \(assetsTable.count)")
return assetsTable.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "seeAllListCell", for: indexPath) as! SeeAllPhotoCell
let list = assetsTable[(indexPath as NSIndexPath).row]
var imageName: String? = (list.poster_image_url)
var image: UIImage? = (images_cache[imageName!])
if image != nil {
debugPrint("Yes Image")
cell.imageView.image = image
} else{
debugPrint("NO Image")
cell.imageView.image = nil
DispatchQueue.main.async(){
let url = NSURL(string: list.poster_image_url)
let data = NSData(contentsOf:url! as URL)
var image = UIImage(data: data as! Data)
DispatchQueue.main.async(execute: {() -> Void in
cell.movieTitle.text = list.name
cell.imageView?.image = image
})
self.images_cache[imageName!] = image
}
}
return cell
}
}
// MARK: - UICollectionViewDelegate
extension SeeAllCollectionView {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
debugPrint("Selected")
let list = assetsTable[(indexPath as NSIndexPath).row]
debugPrint(list.poster_image_url)
debugPrint(list.name)
prefs.set(list.poster_image_url, forKey: "poster_image_url")
prefs.set(list.name, forKey: "name")
prefs.set(list.assets_id, forKey: "VIDEO_ID")
prefs.set(false, forKey: "FLAG")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DetailsChannel") as UIViewController
self.present(vc, animated: true, completion: nil)
}
}
请任何人帮助我该怎么办?
【问题讨论】:
-
显示您的代码,因为您似乎做错了什么。关于从后台线程修改自动布局的错误是明确的,并且已经在 SO 上得到了回答。如果您不显示代码,我们无法猜测在哪里以及为什么。以及为什么要写
cell.layer.rasterizationScale = UIScreen.main.scale。 -
@Larme,您好,我更新了我的帖子。请复习一下,为什么我写 cell.layer 代码是因为我发现有人写这个代码来解决问题。
标签: swift3 uicollectionview uicollectionviewcell uicollectionviewlayout uicollectionviewdelegate