0

When i'm trying to send the POST request with the image, i'm getting this error

Thread 1: signal SiGABART

i think this part

[formData appendPartWithFileData:[NSData dataWithData:[self.images objectAtIndex:indexScroll]] name:@"FileUploadPost" fileName:@"image.jpg" mimeType:@"image/jpeg"]; 

is causing the error

- (IBAction)uploadPhoto:(id)sender {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer new];

        NSDictionary *parameters = @{@"HI": @"Hello"};
        [manager POST:@"http://requestb.in/1c69jt31" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:[NSData dataWithData:[self.images objectAtIndex:indexScroll]] name:@"FileUploadPost" fileName:@"image.jpg" mimeType:@"image/jpeg"];

            [formData appendPartWithFileData:[self.images objectAtIndex:indexOfPage] name:@"FileUploadPost" fileName:@"image.jpg" mimeType:@"image/jpeg"];
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSData * data = (NSData *)responseObject;
            NSLog(@"Success,Response string: %@", [NSString stringWithCString:[data bytes] encoding:NSISOLatin1StringEncoding]);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];

    }
Nicky
  • 97
  • 8
  • @Rob i was doing it like this image=[dict objectForKey:UIImagePickerControllerOriginalImage]; [self.images addObject:image]; – Nicky Mar 18 '15 at 22:16
  • @Rob yeah i know, but is there a way that i can upload the specific image index like i was trying to do above? Because it is only way i can upload the image – Nicky Mar 18 '15 at 22:20
  • @ Rob yeah, now you got me ! Do you know how to get the UIImage by the index? – Nicky Mar 18 '15 at 22:22
  • @Rob Thank you ! u saved me, can you post your comment as a whole answer so i can accept you solution? – Nicky Mar 18 '15 at 22:27
  • BTW, sometimes rather than loading the images into the array, you'd just grab the URL via `UIImagePickerControllerReferenceURL`. Then, when you're building this request, you can retrieve the data of the original asset like so: http://stackoverflow.com/a/25251943/1271826. This is a broader question than you're asking, but if you start seeing memory problems, something like that approach might be more efficient. – Rob Mar 18 '15 at 22:38

1 Answers1

0

If self.images is an array of UIImage objects, you have to use something like UIImageJPEGRepresentation or UIImagePNGRepresentation to extract the NSData for that UIImage object:

- (IBAction)uploadPhoto:(id)sender {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer new];

    NSDictionary *parameters = @{@"HI": @"Hello"};
    [manager POST:@"http://requestb.in/1c69jt31" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:UIImageJPEGRepresentation(self.images[indexScroll], 0.8)] name:@"FileUploadPost" fileName:@"image.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"Success,Response string: %@", responseString);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044