保存图像以进行解析
Saving Image to Parse
我正在处理我的一个应用程序的用户个人资料屏幕。我想让用户 select 来自他 phone 的图像,并能够在应用程序的主屏幕上看到他的个人资料图像。我遇到的问题是保存图像比纯文本需要更长的时间。我想找到一种方法来确保在 segue 将用户带到主屏幕之前保存图像以进行解析。现在只要我点击下一步,segue 就会将用户带到主屏幕。在主屏幕中,无法显示用户的个人资料图片,因为 "saved" 解析的个人资料图片是空的。发生这种情况时,我得到一个零值异常。
注意:当我删除 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)
}
非常感谢您的帮助!
移动这些行:
//takes the user to the MainScreen
self.performSegueWithIdentifier("moveToMainScreen", sender: self)
更高处,进入图像保存的完成块(您现在有 println("picture was uploaded")
。
我正在处理我的一个应用程序的用户个人资料屏幕。我想让用户 select 来自他 phone 的图像,并能够在应用程序的主屏幕上看到他的个人资料图像。我遇到的问题是保存图像比纯文本需要更长的时间。我想找到一种方法来确保在 segue 将用户带到主屏幕之前保存图像以进行解析。现在只要我点击下一步,segue 就会将用户带到主屏幕。在主屏幕中,无法显示用户的个人资料图片,因为 "saved" 解析的个人资料图片是空的。发生这种情况时,我得到一个零值异常。
注意:当我删除 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)
}
非常感谢您的帮助!
移动这些行:
//takes the user to the MainScreen
self.performSegueWithIdentifier("moveToMainScreen", sender: self)
更高处,进入图像保存的完成块(您现在有 println("picture was uploaded")
。