尝试在 iOS 应用程序中保存带有图像的解析对象

Trying to save Parse object with image in iOS app

在视图控制器中,用户可以从照片库中选择一张图片,添加文本,然后在按下按钮后,应用程序应创建一个 PFObject 并将其保存在 background.This 中是我的代码对于按钮操作方法:

-(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) {

    }];
}

一段时间后,调试器出现错误:Error: invalid type for key image, expected map, but got file (Code: 111, Version: 1.2.15)

我一直在 Parse 文档中搜索,但没有关于这个问题的线索。

您一定是为您的列选择了错误的类型。将其更改为文件类型。

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