检索每个用户的个人资料图片解析iOS swift

时间:2015-08-12 11:28:57

标签: ios swift parse-platform

在我的iOS应用中。有像饲料的推特。每个Feed都有用户名,个人资料图片和他添加的推文。

并且我能够检索每个Feed的用户名但无法检索个人资料图片。这是我的代码

let spread:PFObject = self.LiveFeedData.objectAtIndex(indexPath.row) as! PFObject

    var user = spread.objectForKey("spreader") as! PFUser

        user.fetchIfNeededInBackgroundWithBlock { (obj: PFObject?, error: NSError?) -> Void in
            if obj != nil {
                var fetchedUser = obj as! PFUser
                var username = fetchedUser["username"] as! String

                cell.username.text = user.username // This Works FINE

            }
        }

        if let userImageFile = user["photo"] as? PFFile{
        userImageFile.getDataInBackgroundWithBlock {
            (imageData: NSData?, error: NSError?) -> Void in
            if (error != nil) {
                if imageData != nil{
                cell.profileImage.image = UIImage(data:imageData!)
                }else{
                    println("No Data")
                }

            }else{
                println(error)
            }
        }

        }

This iS Tweet Feed

enter image description here

1 个答案:

答案 0 :(得分:3)

如上所述,使用PFImageView可以帮助简化您需要移动到fetch闭包中的代码。

    user.fetchIfNeededInBackgroundWithBlock { (obj: PFObject?, error: NSError?) -> Void in
        if obj != nil {
            var fetchedUser = obj as! PFUser
            var username = fetchedUser["username"] as! String

            cell.username.text = username

            cell.profileImage.file = fetchedUser["photo"] as! PFFile
            cell.profileImage.fetchInBackground()

        }
    }