Adding an Image to Parse Using Swift 2.0

时间:2015-10-29 11:08:56

标签: ios swift parse-platform swift2

I've searched for a long time now and nothing works out for me. I was able to upload user's username, email, password and a sample location to my "User" class on Parse. But I need to upload an image to Parse for user's placeholder profile image. I have the image in my Assets in Xcode (ProfileImagePlaceHolder.png)

How can I upload this image to Parse as User's Profile picture?

 @IBAction func registerTapped(sender: UIButton)
{
    print("Register Page - Register Tapped")

    if txtEmailAddress.text == "" || txtPassword.text == "" || txtPasswordConfirmation.text == ""

    {
        print("Registration fields are not complete.")

        REGISTER_INCOMPLETE_FIELD.show()

    }
    else if txtPassword.text != txtPasswordConfirmation.text
    {

        print("Password Confirmation does not match with Password.")

        PASSWORD_CONFIRMATION_DOESNT_MATCH.show()

    }
    else
    {
        let user = PFUser()

        user.username = txtEmailAddress.text
        user.password = txtPassword.text
        user.email = txtEmailAddress.text
        user["location"] = PFGeoPoint(latitude: 41, longitude: 29)
        user.signUpInBackgroundWithBlock {
            (succeeded: Bool, error: NSError?) -> Void in
            if error != nil {

                print("There is a problem.")

                SOMETHING_IS_WRONG.show()

            } else {
                // Hooray! Let them use the app now.

                print("Successfully registered.")

                SUCCESS_REGISTER.show()

                self.performSegueWithIdentifier(SEGUE_REGISTERED, sender: self)

                //
            }
        }
    }

}

2 个答案:

答案 0 :(得分:1)

Parse有一个关于这个确切主题的iOS教程:https://www.parse.com/docs/ios/guide#files

以下是将文件保存到Parse的代码。您可以将其与PFObject相关联。

// Convert to JPEG with 50% quality
NSData* data = UIImageJPEGRepresentation(imageView.image, 0.5f);
PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:data];

// Save the image to Parse

[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (!error) {
        // The image has now been uploaded to Parse. Associate it with a new object 
        PFObject* newPhotoObject = [PFObject objectWithClassName:@"PhotoObject"];
        [newPhotoObject setObject:imageFile forKey:@"image"];
        [newPhotoObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {
                NSLog(@"Saved");
            }
            else{
                // Error
                NSLog(@"Error: %@ %@", error, [error userInfo]);
            }
        }];
    }
}];

Swift

let imageData = UIImagePNGRepresentation(image)
let imageFile = PFFile(name:"image.png", data:imageData)

var userPhoto = PFObject(className:"UserPhoto")
userPhoto["imageName"] = "My trip to Hawaii!"
userPhoto["imageFile"] = imageFile
userPhoto.saveInBackground()

答案 1 :(得分:0)

我用

解决了这个问题
        let imageToBeUploaded = self.imgProfileImage.image
        let imageData = UIImagePNGRepresentation(imageToBeUploaded!)
        let imageFile = PFFile(name:"profileplaceholder.png", data:imageData!)
        let user = PFUser()

        user.username = txtEmailAddress.text
        user.password = txtPassword.text
        user.email = txtEmailAddress.text
        user["location"] = PFGeoPoint(latitude: 41, longitude: 29)
        user["image"] = imageFile