【问题标题】:Retrieve profile picture of each user parse iOS swift检索每个用户的个人资料图片解析iOS swift
【发布时间】:2015-08-12 11:28:56
【问题描述】:

在我的 iOS 应用中。有推特之类的提要。每个提要都有用户名、头像和他添加的推文。

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

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)
            }
        }

        }

【问题讨论】:

  • 你确定你在 if let 语句中吗?
  • @thefredelement 刚刚检查过。不,我认为问题出在那句话:(
  • 您是试图访问用户照片的用户吗?
  • @thefredelement 我已经用解析截图更新了这个问题。没有任何用户可以看到该应用程序。每个用户都会看到每个提要上传的人的用户名和他的照片
  • if (error != nil) while fetch the PFFile content should be if(error == nil)

标签: ios swift parse-platform


【解决方案1】:

如前所述,使用 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()

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 2018-09-12
    • 1970-01-01
    • 2017-11-16
    相关资源
    最近更新 更多