melkaya
melkaya

Reputation: 1

Adding an Image to Parse Using Swift 2.0

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)

                //
            }
        }
    }

}

Upvotes: 0

Views: 554

Answers (2)

melkaya
melkaya

Reputation: 1

I have solved the issue with

        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

Upvotes: 0

Vijay
Vijay

Reputation: 801

Parse has an iOS tutorial on this exact topic: https://www.parse.com/docs/ios/guide#files

Here is the code to save the file to Parse. You can associate this with your 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]);
            }
        }];
    }
}];

Here in 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()

Upvotes: 1

Related Questions