6

I want to make an iPhone application to send an image to my server.

I want to draw something in iPhone (ex: a signature) as an image to POST binary image to my server (server is JSP). Please tell me what I have to do?

  • how to use iPhone UI?
  • how to make binary data from image, etc.
John Willemse
  • 6,608
  • 7
  • 31
  • 45
MartinJoo
  • 2,784
  • 9
  • 33
  • 39

2 Answers2

13

Firstly you can get an NSData object containing either a PNG or JPEG representation of the image data using the UIImagePNGRepresentation and UIImageJPEGRepresentation functions.

// To get the data from a PNG file
NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);

// To get the data from a JPEG file
NSData *dataForPNGFile = UIImageJPEGRepresentation(yourImage, 0.9f);

(for more information see: UIImage Class Reference)

To finish to upload data from your iPhone to your server you can do this:

- (void)sendImage {
       NSData *postData = [nsdata from your original image];
       NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

       // Init and set fields of the URLRequest
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
       [request setHTTPMethod:@"POST"];
       [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
       [request setHTTPBody:postData];

       NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
       if (connection) {
          // Return data of the request
          NSData *receivedData = [[NSMutableData data] retain];
       }
       [request release];
 }
Yannick Loriot
  • 7,107
  • 2
  • 33
  • 56
  • Thank you for your answer. In above your guide, NSData is gotten from a PNG/JPEG file what exist in anywhere. But I want to draw directly a signature in iPhone and get NSData from it. What I have to do? – MartinJoo Mar 02 '10 at 08:55
  • 2
    what if you also wanted to set a couple of vars on the server in your post request? – joshue Nov 20 '10 at 01:48
  • This question is almost 2 years old! To answer the poster's original and follow-up questions: Inside Yannick's reply you'll see the "yourImage" value. That value is supposed to represent the UIImage of your image. So, if you drew an image onto the screen, and captured a screenshot of the image into a UIImageView named "myImageView", you would be referencing the UIImage contained within that ImageView, via "myImageView.image". The UIImage would then be translated/serialized into the binary data (using the method above) which would be appended to your URLRequest. I hope that clarifies things. :) – user298261 Oct 04 '12 at 21:04
0

Use the drawrect method to do signatures on an UIImage. For that you have to use a UITouch delegate

and use the following to convert your UIImage object to NSData

// To get the data from a PNG file

NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);

// To get the data from a JPEG file

NSData *dataForPNGFile = UIImageJPEGRepresentation(yourImage, 0.9f);
biegleux
  • 13,179
  • 11
  • 45
  • 52
Manoj
  • 1