Send image to server as binary data

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

I want to draw something on the iPhone (for example: signature) as an image for the POST binary image to my server (JSP server). Please tell me what should I do?

  • How to use iPhone UI?
  • how to make binary data from an image, etc.
+4
source share
2 answers

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

// 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); 

(see the UIImage class reference for more information)

To complete the download of 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]; } 
+12
source

Use the drawrect method to create signatures on a UIImage . For this you need to use the UITouch delegate

and to convert your UIImage to NSData

use the following:
 // 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); 
0
source

Source: https://habr.com/ru/post/1302199/


All Articles