【发布时间】:2015-12-23 03:08:53
【问题描述】:
我在 UIView 容器中有一个 UICollectionView,我用它来显示附加到存储在 Parse 中的 PFObject 的图像(作为 PFFile 的数组)。我知道我可以在我当前的实现中同步获取 PFFile/图像(参见下面的同步加载工作代码),但我真的很想把它变成一个异步过程。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("attachedImageCell", forIndexPath: indexPath) as! AttachedImageCollectionViewCell
if let uploadImageFile = attachedImageFiles[indexPath.row] as? PFFile {
do {
var imageData: NSData
try imageData = uploadImageFile.getData()
cell.imageView.image = UIImage(data: imageData)
} catch let err as NSError {
print("error getting image file data: \(err)")
}
/*
//asynchronously getting image data - don't know how to return cell here
uploadImageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error != nil {
//TODO: show error in download
}
if imageData != nil {
cell.imageView.image = UIImage(data: imageData!)
}
})
*/
}
return cell
}
我一直在考虑的一种方法是让AttachedImageCollectionViewCell 对象观察其UIImageView,一旦将UIImage 文件(指针)分配给它,它就会从Parse 中获取PFFile 并解析它到NSData,用于UIImage。
由于我仍处于掌握 Swift 的学习曲线上,我不确定这种方法是否可行。 因此,我愿意倾听任何想法,谢谢!
【问题讨论】:
标签: swift asynchronous parse-platform uiimage uicollectionview