0

i want to upload an UIImage to a server.

the problem i dont know how to add it to my post to the server. until now i sent post with thia:

NSString *reqURL = [NSString stringWithFormat:@"url"];
    reqURL = [reqURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
    NSURLResponse *resp = nil;
    NSError *err = nil;
    NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];

there is any different way to do for image?

YosiFZ
  • 7,792
  • 21
  • 114
  • 221

2 Answers2

0

This answer points to this wrapper framework that should make it easy to do what you want without dealing with HTTP header strings and so on.

Basically, you'll turn your UIImage into an NSData object using a function like UIImagePNGRepresentation() and then upload it over HTTP.

Community
  • 1
  • 1
Joe Osborn
  • 1,145
  • 7
  • 10
0

Use ASIHttpRequest

-(void)uploadImage{
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

    // Upload a file on disk
    [request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"
forKey:@"photo"];

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

  [request setDelegate:self];
  [request setDidFinishSelector:@selector(uploadRequestFinished:)];
  [request setDidFailSelector:@selector(uploadRequestFailed:)];

  [request startAsynchronous];
}
Cyprian
  • 9,423
  • 4
  • 39
  • 73