自从我提出这个问题以来,我做了更多的实验,并认为我现在知道了答案。所有结果都是在 iOS 4.2 上获得的,这就是我所关心的......
首先,我们使用的是UIImageJPEGRepresentation
ala:
NSData *imageData = UIImageJPEGRepresentation(self.selectedImage, 0.9);
这似乎没有给你(大部分)图像中的元数据(EXIF、GPS 等)。很公平,我认为这是众所周知的。
我的测试表明,图像资产的“默认表示”中的 JPEG 将包含所有元数据,包括 EXIF 和 GPS 信息(假设它首先存在)。您可以通过从资产 URL 到资产到资产的默认表示 (ALAssetRepresentation) 获取该图像,然后使用 getBytes 方法/消息检索 JPEG 图像的字节。该字节流中包含上述元数据。
这是我用于此的一些示例代码。它需要一个资产 URL,假定用于图像,并返回带有 JPEG 的 NSData。关于您的使用、代码中的错误处理等方面的警告购买者。
/*
* Example invocation assuming that info is the dictionary returned by
* didFinishPickingMediaWithInfo (see original SO question where
* UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=1000000050&ext=JPG").
*/
[self getJPEGFromAssetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]];
// ...
/*
* Take Asset URL and set imageJPEG property to NSData containing the
* associated JPEG, including the metadata we're after.
*/
-(void)getJPEGFromAssetForURL:(NSURL *)url {
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:url
resultBlock: ^(ALAsset *myasset) {
ALAssetRepresentation *rep = [myasset defaultRepresentation];
#if DEBUG
NSLog(@"getJPEGFromAssetForURL: default asset representation for %@: uti: %@ size: %lld url: %@ orientation: %d scale: %f metadata: %@",
url, [rep UTI], [rep size], [rep url], [rep orientation],
[rep scale], [rep metadata]);
#endif
Byte *buf = malloc([rep size]); // will be freed automatically when associated NSData is deallocated
NSError *err = nil;
NSUInteger bytes = [rep getBytes:buf fromOffset:0LL
length:[rep size] error:&err];
if (err || bytes == 0) {
// Are err and bytes == 0 redundant? Doc says 0 return means
// error occurred which presumably means NSError is returned.
NSLog(@"error from getBytes: %@", err);
self.imageJPEG = nil;
return;
}
self.imageJPEG = [NSData dataWithBytesNoCopy:buf length:[rep size]
freeWhenDone:YES]; // YES means free malloc'ed buf that backs this when deallocated
}
failureBlock: ^(NSError *err) {
NSLog(@"can't get asset %@: %@", url, err);
}];
[assetslibrary release];
}