我正在为我的一个应用程序处理用户配置文件屏幕。我想让用户从他的手机中选择一张图片,并能够在应用程序的主屏幕中看到他的个人资料图片。我遇到的问题是图像的保存时间比纯文本要长。我想找到一种方法来确保在 segue 将用户带到主屏幕之前保存图像以进行解析。现在,只要我单击下一步,segue 就会将用户带到主屏幕。在主屏幕中,用户的个人资料图像无法显示,因为“保存”要解析的个人资料图像是空的。发生这种情况时,我得到一个零值异常。
注意:当我删除 Segue 时 - 应用程序成功保存了用户信息和用户图像。
以下代码是在我对用户名、密码等进行了所有检查之后。
if photoSelected == false {
error = "Please select an image to post"
}
if error != "" {
displayAlert("Cannot Post Image", error: error)
} else {
var post = PFObject(className: "Images")
post["username"] = PFUser.currentUser().username
post.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
if success == false {
self.displayAlert("Could not post Image", error: "Please try again later")
} else {
let imageData = UIImagePNGRepresentation(self.profileImage.image)
let profilePic = PFFile(name: "image.png", data: imageData)
post["profileImage"] = profilePic;
//I even added an activity indicator to stall the app before proceeding to the main page.
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 200, 200))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
self.view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
post.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if success == false {
self.displayAlert("Could not post Image", error: "Please try again later")
} else {
//check
println("picture was uploaded")
}
})
}
})
}
//saves all the user's values username, password, etc.
self.user.save()
//takes the user to the MainScreen
self.performSegueWithIdentifier("moveToMainScreen", sender: self)
}
非常感谢你的帮助!