2

ユーザーがサインアップ時にプロフィール写真を選択するオプションがある解析中のアプリを作成しています。これはそのためのコードです。

    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
        })


    })

}
4

2 に答える 2

0

あなたのエラーはおそらく保存中です:

let imageFile = PFFile(name:"image.png", data:imageData)
profilePictures["profilePhoto"] = imageFile
profilePictures.save()

保存されていない新しい PFFile へのポインターを使用してオブジェクトを保存すると、エラーが発生します。最初に imageFile.saveInBackground を実行し、コールバックを使用して imageFile を profilePictures に割り当て、次に profilePictures を保存する必要があります。

Parse のデータストアで、profilePictures オブジェクトのキー「profilePhoto」に値がないことを確認できます。

于 2015-06-11T16:58:16.220 に答える