I have images saved in my application Documents directory in .jpg format with EXIF data. My problem is, whenever I try to convert the .jpg files into NSData
to be uploaded using HTTP POST, the uploaded image loses all its EXIF data.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",imageName]];
NSData *jpegImageData = [NSData dataWithContentsOfFile:imagePath];
How can I upload my .jpg files from Documents directory with their EXIF intact?
To keep the EXIF metadata of jpg file. I worked with the solution:
- (void)uploadImage:(NSString *)imagePath {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Repair upload data
UIImage *image = [UIImage imageNamed:imagePath];
NSData *rawDataImage = UIImageJPEGRepresentation(image, 1.0f);
NSString *base64EncodedString = @"";
if ([rawDataImage respondsToSelector:@selector(base64EncodedStringWithOptions:)])
{
base64EncodedString = [rawDataImage base64EncodedStringWithOptions:0];
}
else
{
base64EncodedString = [rawDataImage base64Encoding];
}
NSString *uploadBody = [NSString stringWithFormat:@"{\"imageData\": \"%@\"}", base64EncodedString];
// Post uploadBody to server
NSString *serverUrl = @"Your_server";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverUrl]];
[request addValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *msgLength = [NSString stringWithFormat:@"%lu", uploadBody.length];
[request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[uploadBody dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection connectionWithRequest:request delegate:<your_delegate OR nil>];
});
}
Hope this help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With