Reputation: 9143
I have learned two method(Probably there is more..) to upload image on server..
1) Create NSData and add it into request body
2) Create Byte Array and send it in json like simple array
1) Create NSData and add it into request body
Create NSData from the image using this code
NSData *imageData = UIImagePNGRepresentation(myimage);
And than add it in request body
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[body appendData:[NSData dataWithData:imageData]];
[request setHTTPBody:body];
Perform request and Image will be uploaded on server.
2) Create Byte Array and send it in json like simple array
Create Byte array of image
NSData *imageData = UIImagePNGRepresentation(myimage);
const unsigned char *bytes = [imageData bytes];
NSUInteger length = [imageData length];
NSMutableArray *byteArray = [NSMutableArray array];
for (NSUInteger i = 0; i<length; i++)
{
[byteArray addObject:[NSNumber numberWithUnsignedChar:bytes[i]]];
}
And than send it in request using JSON like any other array
But which is the better solution or there is another faster way..
Upvotes: 0
Views: 1355
Reputation: 62676
Both ways are O(n), but my naive look at the NSData approach says it does 1 less copy pass over the data vs the byte array approach. The best (only?) way to discover the difference (and whether the difference matters to the user experience, which is highly unlikely) is to conduct a test.
The test can be simplified if the body sizes of the two approaches are similar, which they should be. This will eliminate the time it takes to do an actual post as a variable. Then just measure the time it takes to prepare the post each way.
Upvotes: 0
Reputation: 1453
The simplest and the fastest way will be to send the image using normal http POST, and html.
Because you are using NSMutableRequest to send the POST request in any case. So only the thing matters is the size of the data and the processing required to get the original image. So to achieve some remarkable improvement in performance you have to make sure that you are using the smallest number of block to send the item over web , with smallest processing overhead.
Hope this article can help you make a choice.
If you want , you can further reduce the size using some compression scheme like this, if you have fixed size concerns.
Upvotes: 1