如何在 iPhone 上保存带有 EXIF(GPS) 和方向的照片?
我正在使用 writeImageToSavedPhotosAlbum:metadata:completionBlock: 将图像保存到相机胶卷(GPS 数据在传递给元数据的字典中)。然而,照片方向错误(因为我在拍照时翻转设备)。
还有另一个函数 writeImageToSavedPhotosAlbum:orientation:completionBlock: 但我无法传递 EXIF 数据。
根据文档,有 kCGImagePropertyOrientation 属性手动设置方向,但我在检测当前方向时遇到问题拍照并保存时使用设备。
有谁实现了用 EXIF 数据和正确的方向保存图片吗?任何帮助将不胜感激。
这是我的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[self dismissModalViewControllerAnimated:YES];
[imageView setImage:image];
if(sourceCamera) {
//EXIF and GPS metadata dictionary
(...)
//saving image
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
[al writeImageToSavedPhotosAlbum:[image CGImage]
metadata:dict
completionBlock:^(NSURL *assetURL, NSError *error) {
if (error == nil) {
NSLog(@"saved");
} else {
NSLog(@"error");
}
}];
[al release];
}
}
祝愿,
a。
I'm using writeImageToSavedPhotosAlbum:metadata:completionBlock: to save images to the camera roll (GPS data is in dictionary passed to metadata). However pictures are misoriented (as I flip the device while taking the pictures).
There's another function writeImageToSavedPhotosAlbum:orientation:completionBlock: but I won't be able to pass EXIF data.
According to the documentation, there's kCGImagePropertyOrientation property to set orientation manually but I'm having problems with detecting current orientation of the device while taking the picture and saving it.
Has anyone achieved saving picture with EXIF data and proper orientation? Any help would be very appreciated.
Here's my code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[self dismissModalViewControllerAnimated:YES];
[imageView setImage:image];
if(sourceCamera) {
//EXIF and GPS metadata dictionary
(...)
//saving image
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
[al writeImageToSavedPhotosAlbum:[image CGImage]
metadata:dict
completionBlock:^(NSURL *assetURL, NSError *error) {
if (error == nil) {
NSLog(@"saved");
} else {
NSLog(@"error");
}
}];
[al release];
}
}
Best wishes,
a.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置
kCGImagePropertyOrientation
时,您需要将UIImageOrientation值映射到相应的EXIF方向值。该映射是:请注意, UIImageOrientationLeft 和 UIImageOrientationRight 与您可能期望的相反 文档;如果将方向应用到直立图像,则会得到小样本图像,而不是应用方向时给出直立图像的原始图像数据的方向。
You need to map the UIImageOrientation values to the corresponding EXIF orientation values when setting
kCGImagePropertyOrientation
. That mapping is:Note that UIImageOrientationLeft and UIImageOrientationRight are the opposite of what you might expect from the documentation; the little sample images are what you get if you apply the orientation to an upright image, rather than the orientation of the raw image data that will give an upright image when the orientation is applied.