我有一个PFQueryTableViewController,其中填充了来自不同用户的评论。每个用户都有一个配置文件图片存储在解析数据库中。将每个评论加载到单元格后,我查询PFUser类以检索发布评论的用户的个人资料图片并将其添加到单元格中。我还使用PFCachePolicy将个人资料图片缓存到设备的内存中,以便使用新的个人资料图片显示新的单元格更加平滑。

然而,这种情况并非如此。当用户发表新评论并添加新单元格时,个人资料图片会随机播放,大约需要两秒钟左右的时间来更新正确的图片(可能是因为重新查询了表格并更新了表格)。我正在尝试实现类似于iMessage或WhatsApp的功能,其中个人资料图片在单元格中保持“固定”。

我不确定是什么问题,或者是否有更好的方法来做到这一点?

    // get objectId of the user who posted a comment
    let senderId = object?["Users"]!.objectId as String!

    // query PFUser class using senderId to retrieve profile picture
    var senderImage:PFQuery = PFUser.query()!
    senderImage.cachePolicy = PFCachePolicy.CacheThenNetwork
    senderImage.getObjectInBackgroundWithId(senderId){
        (sender: PFObject?, error: NSError?) -> Void in

        if error == nil && sender?.objectForKey("profilePicture") != nil {
            let thumbnail = sender?.objectForKey("profilePicture") as? PFFile
            thumbnail?.getDataInBackgroundWithBlock({
                (imageData: NSData?, error: NSError?) -> Void in
                if error == nil {
                    imageView.image = UIImage(data:imageData!)
                } else {
                    println(error)
                }
            })
        }
    }

最佳答案

这是因为更新UIImageView时,您不必等到图像加载完成。尝试使用以下代码:

     var query = PFQuery(className:"Users")


            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in

                if error == nil {
                    // The find succeeded.
                    self.scored = objects!.count
                    // Do something with the found objects
                    if let objects = objects as? [PFObject] {
                        for object in objects {




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

                                        self.imageArray.append(image!)

                                    }
                                }



                                dispatch_async(dispatch_get_main_queue()) {
                                   //don't reload image view here!
                                }

                            }





                        }
                    }
                } else {
                    // Log details of the failure
                    print("Error: \(error!) \(error!.userInfo)")
                }

                dispatch_async(dispatch_get_main_queue()) {

                 //wait until here to reload the image view

if self.imageArray.isEmpty == false {
//image array is not empty

    self.ImageView.image = imageArray.first

}
else {
//no images found in parse
}



            }

关于ios - 单元格中的个人资料图片未正确更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30876787/

10-11 22:38
查看更多