gpt4 book ai didi

ios - 将图像从 Parse 无延迟地加载到 UICollectionView 单元格

转载 作者:可可西里 更新时间:2023-11-01 02:18:59 26 4
gpt4 key购买 nike

我有一个非常复杂的问题,我认为具有广泛异步知识的人可以帮助我。

我有一个 collectionView,其中填充了“图片”对象。这些对象是从自定义类创建的,然后再次使用从 Parse(从 PFObject)获取的数据填充这些对象。

首先查询Parse

func queryParseForPictures() {
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, err: NSError?) -> Void in
if err == nil {
print("Success!")
for object in objects! {
let picture = Picture(hashtag: "", views: 0, image: UIImage(named: "default")!)
picture.updatePictureWithParse(object)
self.pictures.insert(picture, atIndex: 0)
}
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
self.filtered = self.pictures
self.sortByViews()
self.collectionView.reloadData()
}
}
}
}

现在我还在 PFObject 中获得了一个 PFFile,但是看到将 PFFile 转换为 NSData 也是一个异步调用(同步会阻止整个事情......),我无法弄清楚如何正确加载它。函数“picture.updatePictureWithParse(PFObject)”更新除 UIImage 之外的所有其他内容,因为其他值是基本字符串等。如果我也在此函数中从 PFFile 获取 NSData,则“collectionView.reloadData()”将触发在图片加载之前关闭,我最终会得到一堆没有图像的图片。除非我在之后或其他什么情况下强制重新加载。因此,我将 PFFile 存储在对象中以备将来在 updatePictureWithParse 中使用。这是 Picture 类内部的 super 简单函数:

func updateViewsInParse() {
let query = PFQuery(className: Constants.ParsePictureClassName)
query.getObjectInBackgroundWithId(parseObjectID) { (object: PFObject?, err: NSError?) -> Void in
if err == nil {
if let object = object as PFObject? {
object.incrementKey("views")
object.saveInBackground()
}
} else {
print(err?.description)
}
}
}

为了半正经地获取图像,我在 cellForItemAtIndexPath 中实现了图像加载,但这太糟糕了。对于前 10 个或其他任何内容都很好,但是当我向下 ScrollView 时,它会滞后很多,因为它必须从 Parse 中获取下一个单元格。请参阅下面的实现:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Constants.PictureCellIdentifier, forIndexPath: indexPath) as! PictureCell

cell.picture = filtered[indexPath.item]

// see if image already loaded
if !cell.picture.loaded {
cell.loadImage()
}
cell.hashtagLabel.text = "#\(cell.picture.hashtag)"
cell.viewsLabel.text = "\(cell.picture.views) views"
cell.image.image = cell.picture.image

return cell
}

实际的获取是在单元格内:

func loadImage() {
if let imageFile = picture.imageData as PFFile? {
image.alpha = 0
imageFile.getDataInBackgroundWithBlock { [unowned self] (imageData: NSData?, err: NSError?) -> Void in
if err == nil {
self.picture.loaded = true
if let imageData = imageData {
let image = UIImage(data: imageData)
self.picture.image = image
dispatch_async(dispatch_get_main_queue()) {
UIView.animateWithDuration(0.35) {
self.image.image = self.picture.image
self.image.alpha = 1
self.layoutIfNeeded()
}
}
}
}
}
}
}

希望您能理解我的问题。在单元格内获取图像出队是非常糟糕的。此外,如果这几个片段没有给出完整的图片,请参阅该项目的 github 链接: https://github.com/tedcurrent/Anonimg

谢谢大家!

/T

最佳答案

可能有点晚了,但是当从 UICollectionView 中的数据库加载 PFImageView 时,我发现这种方法效率更高,尽管我不完全确定原因。我希望它有所帮助。在 cellForItemAtIndexPath 中使用代替 cell.loadImage() 函数。

if let value = filtered[indexPath.row]["imageColumn"] as? PFFile {
if value.isDataAvailable {
cell.cellImage.file = value //assign the file to the imageView file property
cell.cellImage.loadInBackground() //loads and does the PFFile to PFImageView conversion for you
}
}

关于ios - 将图像从 Parse 无延迟地加载到 UICollectionView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32798981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com