如何使用swift从解析中将图像设置为个人资料图片

时间:2015-10-02 00:42:49

标签: ios image swift parse-platform

从解析中检索文本很简单,但是当我尝试使用图像时,我似乎无法匹配这两种类型以使其以任何方式工作。代码如下:

@IBOutlet weak var realProfilePictureBackground: UIImageView!
@IBOutlet weak var realProfilePicture: UIImageView!
@IBOutlet weak var realNameLabel: UILabel!
@IBOutlet weak var realUsernameLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    let userFirstName = (PFUser.currentUser()?.objectForKey("firstName") as? String)!
    let userlastName = (PFUser.currentUser()?.objectForKey("firstName") as? String)!
    let userUsername = (PFUser.currentUser()?.objectForKey("username") as? String)!
    let profilePicture = (PFUser.currentUser()?.objectForKey("profilePicture") as? PFFile)!


    realNameLabel.text = userFirstName + " " + userlastName
    realUsernameLabel.text = userUsername
    realProfilePictureBackground = profilePicture
}

字符串工作正常,但图像出错: 我认为Cannot assign a value of type 'PFFile to a value of type 'UIImageView!'是有道理的,但我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用:

if let userPicture = PFUser.currentUser()?.objectForKey("profilePicture")! as! PFFile {
   userPicture.getDataInBackgroundWithBlock({
      (imageData: NSData!, error NSError!) -> Void in
         if (error == nil) {
            let image = UIImage(data:imageData)
            //self.ImageArray.append(image)
           dispatch_async(dispatch_get_main_queue()){() -> Void in
               realProfilePictureBackground.image = image
            }
         }
      })
}

请参阅此POST:How to convert an PFFile to an UIImage with swift?