这是我的代码:
@IBAction func setPicture(sender: UIButton) {
var imagePicker: UIImagePickerController = UIImagePickerController()
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.delegate = self
let profileImage: PFFile = user["profileImage"] as! PFFile
profileImage.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let image: UIImage = UIImage(data: imageData!)!
self.profileImageView.image = image
}
}
self.presentViewController(imagePicker, animated: true, completion: nil)
}
错误在let profileImage:PFFile = user [“ profileImage”]行上。它不会因为用户而建立。帮助赞赏!
最佳答案
我认为这可能对您有用。
@IBAction func setPicture(sender: UIButton) {
var imagePicker: UIImagePickerController = UIImagePickerController()
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.delegate = self
//getting the current user
var user = PFUser.currentUser();
let profileImage: PFFile = user["profileImage"] as! PFFile
profileImage.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let image: UIImage = UIImage(data: imageData!)!
self.profileImageView.image = image
}
}
self.presentViewController(imagePicker, animated: true, completion: nil)
}