iOS - 上传完整的 EXIF 图片
iOS - Uploading image with EXIF intact
我在我的应用程序文档目录中保存了带有 EXIF 数据的 .jpg 格式的图像。我的问题是,每当我尝试将 .jpg 文件转换为 NSData
以使用 HTTP POST 上传时,上传的图像会丢失其所有 EXIF 数据。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",imageName]];
NSData *jpegImageData = [NSData dataWithContentsOfFile:imagePath];
如何从 Documents 目录上传我的 .jpg 文件且其 EXIF 完好无损?
保留jpg文件的EXIF元数据。我使用了解决方案:
- (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>];
});
}
希望对您有所帮助。
我在我的应用程序文档目录中保存了带有 EXIF 数据的 .jpg 格式的图像。我的问题是,每当我尝试将 .jpg 文件转换为 NSData
以使用 HTTP POST 上传时,上传的图像会丢失其所有 EXIF 数据。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",imageName]];
NSData *jpegImageData = [NSData dataWithContentsOfFile:imagePath];
如何从 Documents 目录上传我的 .jpg 文件且其 EXIF 完好无损?
保留jpg文件的EXIF元数据。我使用了解决方案:
- (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>];
});
}
希望对您有所帮助。