将多个图像上传到Parse的正确方法

时间:2014-04-28 18:11:34

标签: ios objective-c json parse-platform

我使用名为ELCImagePicker的开源自定义类保存多个图像。使用ELCImagePicker的代码位于特定选项卡的控制器的ViewDidLoad方法中。

ELCImagePickerController *imagePicker = [[ELCImagePickerController alloc]initImagePicker];
        imagePicker.maximumImagesCount = 20;
        imagePicker.imagePickerDelegate = self;

        [self presentViewController:imagePicker animated:YES completion:nil];

目前,我用来将图像本身上传到Prase的方法是作为服务的后端

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    NSData *fileData;
    NSString *fileName;
    NSString *fileType;

    for(id image in info){

        fileData = UIImagePNGRepresentation(image);
        fileName = @"image.png";

        PFFile *imageFile = [PFFile fileWithName:fileName data:fileData];
        [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error){
                UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Image Upload Error" message:@"please try sending your image again" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [errorAlert show];
            }else{
                PFObject *message = [PFObject objectWithClassName:@"Message"];
                [message setObject:imageFile forKey:@"imageFile"];
                [message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];
                [message setObject:[[PFUser currentUser] username] forKey:@"username"];
                [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                    if (error){
                        UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Image Upload Error" message:@"please try sending your image again" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                        [errorAlert show];
                    }
                }];


            }
        }];
    }

}

首先,info是一个包含所选多个图像的数组。我目前正在通过数组进行枚举,而不是使用Parse的自定义API来上传图像。到目前为止,我收到一条错误消息:“ 2014-04-28 11:03:58.574 One Take [42357:90b] - [__ NSDictionaryM CGImage]:无法识别的选择器发送到实例0xe9749b0 2014-04-28 11:03:58.582 One Take [42357:90b] 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSDictionaryM CGImage]:无法识别的选择器发送到实例0xe9749b0' 首先抛出调用堆栈:

我想知道什么是正确的方法来上传多个图像进行解析,我不确定它是否是一个很好的做法,甚至做我目前正在做的事情。

1 个答案:

答案 0 :(得分:4)

解析部分不是抛出错误,id image in info是错误的。它返回字典,因此您尝试将字典转换为UIImage。

应该是:

for (NSDictionary * dictionary in info) {
    UIImage *image = dictionary[UIImagePickerControllerOriginalImage]; //ELC packages its dictionaries with the same key as UIImagePicker
    fileData = UIImagePNGRepresentation(image);

    // ... the rest of your code ... //

}

您的解析保存不一定是个问题;然而,它们可能会有问题,因为它们无法跟踪哪张照片触发了错误。也许在单独的数组中跟踪错误照片,然后通知用户哪些照片无法上传。然后给他们选择再试一次这些照片。