3

I'm trying upload image to server using AFNetworking with PUT request.-

     UIImage* snap = info[UIImagePickerControllerOriginalImage];
      NSData *imageData = UIImageJPEGRepresentation(snap, 0.3);
      NSMutableString * fullPath = [NSMutableString stringWithString:API_BASE_URL];
    [fullPath appendFormat:@"%@%@",API_VERSION,req];
    NSURL * url = [NSURL URLWithString:fullPath];

   AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];

    [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@", @kPublicKey] forHTTPHeaderField:@"X-API-KEY"];
    [manager.requestSerializer setValue:bodyStr forHTTPHeaderField:@"X-API-DATA"];
    NSString *URLString = fullPath;

    NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:URLString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"media" fileName:@"upload.jpg" mimeType:@"image/jpeg"];
    } error:nil];


   AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {

      //success
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

         NSLog(@"failure...");

    }];

    [requestOperation start];

I'm taking image using iPhone camera and upload it to server but it takes too much time to process and images uploaded on server are in huge size(~10-12MB) although i'm trying to compress the image? What i'm doing wrong?Any suggestion or sample code would be appreciated.

Blios
  • 719
  • 1
  • 16
  • 30
  • For what? Resizing the source image? – Wain Apr 07 '14 at 09:56
  • And what size is the source image? A 10 / 12 MB image will take time to upload. Your only option is to limit to WiFi upload / resize the image so the data is smaller – Wain Apr 07 '14 at 10:02
  • I'm taking image from iPhone camera and upload it to server.so don't know the size. – Blios Apr 07 '14 at 10:03
  • I've edited the question please have a look. – Blios Apr 07 '14 at 10:13
  • possible duplicate of [UIImage: Resize, then Crop](http://stackoverflow.com/questions/603907/uiimage-resize-then-crop) – Wain Apr 07 '14 at 10:18
  • Is it only matter of resizing image?Is my approach to upload file correct ? – Blios Apr 07 '14 at 10:21
  • yeah, looks ok. I'm assuming you have no errors in the server log and that the upload does complete (it just takes a while)... – Wain Apr 07 '14 at 10:22
  • yes it uploaded to server and i got an URL in response but when i try to check that url on browser i did not get it.. and developer on server side says your images were very big. – Blios Apr 07 '14 at 10:25
  • Test with a small image downloaded from the web and picked from the user album instead of taken with the camera – Wain Apr 07 '14 at 10:29
  • just one more thing please..if i decide to scale image then what should the new size(CGSize) of image? – Blios Apr 07 '14 at 10:46
  • That depends entirely on what you're going to use it for, so I can't say... What size is the server dev expecting?? – Wain Apr 07 '14 at 10:48

2 Answers2

0
NSData *imageData = UIImageJPEGRepresentation(snap, 1.0);

The second argument in UIImageJPEGRepresentation denotes the compression quality of the image.As per Apple documentation :

compressionQuality :
The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).

Try reducing this to a number which balances the quality of image and speed of upload.

Shubham
  • 91
  • 3
0
// manager needs to be init'd with a valid baseURL
NSURL *baseURL = [AfarHTTPSessionManager sharedManager].baseURL;
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];

NSData *imageData = UIImageJPEGRepresentation(draftHighlight.largeImage, 1);

// need to pass the full URLString instead of just a path like when using 'PUT' or 'POST' convenience methods
NSString *URLString = [NSString stringWithFormat:@"%@%@", baseURL, _the_rest_of_your_path];
NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:kCreateHighlightAPIKeyImage fileName:@"highlight_image.jpg" mimeType:@"image/jpeg"];
}];

// 'PUT' and 'POST' convenience methods auto-run, but HTTPRequestOperationWithRequest just
// sets up the request. you're responsible for firing it.
AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // failure
}];

// fire the request
[requestOperation start];
Erhan
  • 908
  • 8
  • 19