【问题标题】:How to set image as a profile picture from parse with swift如何将图像设置为使用 swift 解析的个人资料图片
【发布时间】:2015-10-02 00:42:48
【问题描述】:

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

@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!' 我认为这是有道理的,但我该如何解决呢?

【问题讨论】:

    标签: ios image swift parse-platform


    【解决方案1】:

    使用:

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

    参考这个帖子:How to convert an PFFile to an UIImage with swift?

    【讨论】:

    • 那么我将如何使用此代码将其设置为 IBOutlet?
    • 我假设您所指的 IBOutlet 是 UIImageView。从 imageData 获取图像后,您可以分派回主队列。请参阅上面的编辑代码。
    猜你喜欢
    • 2017-09-30
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多