单元格中的个人资料图片未正确更新

时间:2015-06-16 19:45:32

标签: ios swift parse-platform

我有一个PFQueryTableViewController,由不同用户的评论填充。每个用户都有一个存储在Parse数据库中的个人资料图片。将每个注释加载到单元格后,我查询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)
                }
            })
        }
    }

1 个答案:

答案 0 :(得分:1)

那是因为你在更新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
}



            }