ios - 与解析异步

标签 ios swift asynchronous parse-platform

我正在构建一个基于图片的应用程序。因此,像 Instagram 那样异步检索所有图像对我来说非常重要。我明白这个功能...

    var query = PFQuery(className: "Post")
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if let objects = objects as! [PFObjects] {
            for object in objects {
                objectsArray.append(object)
            }
        }
    }

...是异步的。但我想要一种将图像从 Parse 异步加载到表中的方法,以便在滚动时加载图像。

最佳答案

你应该看看 PFImageView 的函数 loadInBackground()

例如,如果您使用带有 PFTableViewCell 的 PFTableViewController,您可以执行以下操作

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell!
    if cell == nil {
        cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
    }

    if let name = object?["name"] as? String {
        cell.nameLbl.text = name
    }

    var initialThumbnail = UIImage(named: "placeholder")
    cell.imgView.image = initialThumbnail
    if let thumbnail = object?["photo"] as? PFFile {
        cell.imgView.file = thumbnail
        cell.imgView.loadInBackground()
    }

    return cell
}

PFTableViewCell 有

class CustomCell: PFTableViewCell {
    @IBOutlet weak var nameLbl: UILabel!
    @IBOutlet weak var imgView: PFImageView!
}

同样来自另一个 SO reply ,你可以试试这个:

let userImageFile = userPhoto["imageFile"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
    (imageData: NSData!, error: NSError!) -> Void in
    if !error {
        let image = UIImage(data:imageData)
    }
}

关于ios - 与解析异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34915219/

相关文章:

c# - 异步 tcp 服务器内存不断增长,有时也会在 endreceive 上出现 10054 错误

c# - 如何避免在较新的应用程序版本中覆盖数据文件

iphone - 如何用zxing读取标准条码(EAN)? iOS

ios - 如何使用 iOS 读取 CSV 文件

ios - Firebase 应用程序配置失败的 IOS

ios - 关于在现有 iOS 应用程序中集成 Firebase 的链接问题

ios - 调整大小的UITableViewCell : toggling UITextView in UITableViewCell by using auto layouts

ios - 添加观察在 Swift 4 中不起作用

c# - 什么是异步回调?

javascript - 在 Knockout.js 中异步应用绑定(bind)