mvasco
mvasco

Reputation: 5107

Trying to save Parse object with image in iOS app

In a view controller, the user can pick an image from the photo library, add a text and then after pressing a button,the app should create a PFObject and save it in the background.This is my code for the button action method:

-(IBAction)sendPressed:(id)sender
{


    NSLog(@"boton subir cadena pulsado");

    //Upload a new picture
    NSData *pictureData = UIImagePNGRepresentation(self.imageView.image);

    PFFile *file = [PFFile fileWithName:@"img" data:pictureData];
    [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

        if (succeeded){
            NSLog(@"IMAGEN CARGADA");

            //Add the image to the object, and add the comments
            PFObject *imageObject = [PFObject objectWithClassName:@"cadenas"];
            [imageObject setObject:file forKey:@"image"];

            [imageObject setObject:self.commentTextField.text forKey:@"chain_name"];


            [imageObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

                if (succeeded){
                    //Go back to the wall
                    [self.navigationController popViewControllerAnimated:YES];
                }
                else{
                    NSString *errorString = [[error userInfo] objectForKey:@"error"];
                    [self showErrorView:errorString];
                }
            }];
        }
        else{
            NSString *errorString = [[error userInfo] objectForKey:@"error"];
            [self showErrorView:errorString];
        }



    } progressBlock:^(int percentDone) {

    }];
}

After a while, an error appears at the debugger: Error: invalid type for key image, expected map, but got file (Code: 111, Version: 1.2.15)

I have been searching in the Parse documents but no clue about the issue.

Upvotes: 0

Views: 117

Answers (1)

Dheeraj Singh
Dheeraj Singh

Reputation: 5183

you must have selected wrong type for your column. Change it to File type.

            NSData *imageData = UIImagePNGRepresentation(YourImageView.image);

            PFFile *imageFile = [PFFile fileWithData:imageData];
            [userObject setObject:imageFile forKey:@"YOUR_KEY"];
            [userObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if(succeeded)
                {
                    NSLog(@"Object saved");
                }else
                {
                    NSLog(@"Error : %@",error);
                }
            }];

Upvotes: 1

Related Questions