6
NSData *imageData = UIImagePNGRepresentation(image);

How send imageData using POST?

abg
  • 2,002
  • 7
  • 39
  • 63

3 Answers3

11

The following code should help

NSData *imageData = UIImagePNGRepresentation(image);

NSURL *yourURL = ...
NSMutableURLRequest *yourRequest = [NSMutableURLRequest requestWithURL:yourURL 
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                       timeoutInterval:60.0];
//Set request to post
[yourRequest setHTTPMethod:@"POST"];

//Set content type 
[yourRequest setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];

// Set authorization header if required
...

// set data
[yourRequest setHTTPBody:imageData];


// create connection and set delegate if needed
NSURLConnection *yourConnection = [[NSURLConnection alloc] initWithRequest:yourRequest 
                                                                  delegate:self
                                                          startImmediately:YES];

Please note that, it is assumed that you are using ARC.

Mert
  • 6,025
  • 3
  • 21
  • 33
2

You can check this answer, if you are okay with using NSURLConnection https://stackoverflow.com/a/10750428/591951

This post explains how to POST an Audio file, but you can upload any file using the same method

Community
  • 1
  • 1
Krrish
  • 2,256
  • 18
  • 21
1

You can use ASIHTTPRequest library: http://allseeing-i.com/ASIHTTPRequest/

Then it's pretty easy:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

// Upload an NSData instance
[request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

Info on how to use: http://allseeing-i.com/ASIHTTPRequest/How-to-use

Michal Dymel
  • 4,312
  • 3
  • 23
  • 32
  • From http://allseeing-i.com/ASIHTTPRequest/How-to-use : "Please note that I am no longer working on this library - you may want to consider using something else for new projects. :)" So I do not advise to use the library. – Mert Sep 13 '12 at 11:00