简体   繁体   English

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

[英]Proper way to upload multiple images to Parse

I am saving multiple images by using an open source custom class called ELCImagePicker. 我使用名为ELCImagePicker的开源自定义类保存多个图像。 The code to use the ELCImagePicker is found within the ViewDidLoad method of the controller for the specific tab. 使用ELCImagePicker的代码位于特定选项卡的控制器的ViewDidLoad方法中。

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

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

Currently, the method I am using to upload the images itself to Prase which is a backend as a service is 目前,我使用的方法将图像本身上传到作为服务后端的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];
                    }
                }];


            }
        }];
    }

}

First and foremost, info is an array which contains the multiple images that are selected. 首先,info是一个包含所选多个图像的数组。 I am currently enumerating through the array and than using Parse's custom API to upload the images. 我目前正在通过数组进行枚举,而不是使用Parse的自定义API来上传图像。 So far I am getting an error message which reads: " 2014-04-28 11:03:58.574 One Take[42357:90b] -[__NSDictionaryM CGImage]: unrecognized selector sent to instance 0xe9749b0 2014-04-28 11:03:58.582 One Take[42357:90b] 到目前为止,我收到一条错误消息:“ 2014-04-28 11:03:58.574 One Take [42357:90b] - [__ NSDictionaryM CGImage]:无法识别的选择器发送到实例0xe9749b0 2014-04-28 11:03: 58.582一拍[42357:90b] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM CGImage]: unrecognized selector sent to instance 0xe9749b0' 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSDictionaryM CGImage]:无法识别的选择器发送到实例0xe9749b0' First throw call stack: " 第一次抛出调用堆栈:

I am wondering what is a proper method in order to upload multiple images to parse, I'm not sure if its a good practice to even do what I am currently doing. 我想知道什么是一个适当的方法来上传多个图像来解析,我不确定它是一个好的做法,甚至做我目前正在做的事情。

The parse part isn't what's throwing the error, the id image in info is wrong. 解析部分不是抛出错误,信息中的id image in info是错误的。 It returns dictionaries, so you're trying to convert a dictionary to a UIImage. 它返回字典,因此您尝试将字典转换为UIImage。

It should be: 它应该是:

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 ... //

}

Your parse saves aren't necessarily a problem; 你的解析保存不一定是个问题; however, they might be problematic because they don't keep track of which photo triggered the error. 然而,它们可能会有问题,因为它们无法跟踪哪张照片触发了错误。 Perhaps keep track of the error photos in a separate array, then notify the user which photos failed to upload. 也许在单独的数组中跟踪错误照片,然后通知用户哪些照片无法上传。 Then give them the option to try again on just those photos. 然后让他们选择再次尝试这些照片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM