ユーザーがサインアップ時にプロフィール写真を選択するオプションがある解析中のアプリを作成しています。これはそのためのコードです。
var profilePictures = PFObject(className: "ProfilePictures")
let imageData = UIImagePNGRepresentation(self.profileImage.image)
let imageFile = PFFile(name:"image.png", data:imageData)
profilePictures["profilePhoto"] = imageFile
profilePictures["user"] = usernameField.text
profilePictures.save()
後で、選択したプロファイル画像を UIImageView に入力する必要がある画面が表示されます。
これは、アプリケーション自体が完全に停止して再起動されるまで機能します。
その後、PFFile は nil として検出され、「オプションの値のラップ解除中に予期せず nil が見つかりました」というエラーが表示されます。
画像を表示するためのコードは次のとおりです。
override func viewDidAppear(animated: Bool) {
var query = PFQuery(className: "ProfilePictures")
query.whereKey("user", equalTo: PFUser.currentUser()?.username)
query.findObjectsInBackgroundWithBlock({
(success, error) -> Void in
let userImageFile = profilePictures["profilePhoto"] as! PFFile
//error is on the above line
userImageFile.getDataInBackgroundWithBlock({
(imageData: NSData?, error) -> Void in
var image = UIImage(data: imageData!)
self.profileImage.image = image
})
})
}