我正在处理其中一个应用的用户个人资料屏幕。我希望允许用户从他的手机中选择一个图像,并能够在应用程序的主屏幕中看到他的个人资料图像。我遇到的问题是图像比纯文本需要更长时间才能保存。我想找到一种方法来确保在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)
}
非常感谢你的帮助!
答案 0 :(得分:2)
移动以下行:
//takes the user to the MainScreen
self.performSegueWithIdentifier("moveToMainScreen", sender: self)
更高,进入图像保存的完成块(现在你有println("picture was uploaded")
。