如何在 iPhone 上保存带有 EXIF(GPS) 和方向的照片?

发布于 2024-11-23 20:31:20 字数 1745 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

比忠 2024-11-30 20:31:20

设置kCGImagePropertyOrientation时,您需要将UIImageOrientation值映射到相应的EXIF方向值。该映射是:

UIImageOrientationUp:             1
UIImageOrientationDown:           3
UIImageOrientationLeft:           8
UIImageOrientationRight:          6
UIImageOrientationUpMirrored:     2
UIImageOrientationDownMirrored:   4
UIImageOrientationLeftMirrored:   5
UIImageOrientationRightMirrored:  7

请注意, UIImageOrientationLeft 和 UIImageOrientationRight 与您可能期望的相反 文档;如果将方向应用到直立图像,则会得到小样本图像,而不是应用方向时给出直立图像的原始图像数据的方向。

You need to map the UIImageOrientation values to the corresponding EXIF orientation values when setting kCGImagePropertyOrientation. That mapping is:

UIImageOrientationUp:             1
UIImageOrientationDown:           3
UIImageOrientationLeft:           8
UIImageOrientationRight:          6
UIImageOrientationUpMirrored:     2
UIImageOrientationDownMirrored:   4
UIImageOrientationLeftMirrored:   5
UIImageOrientationRightMirrored:  7

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文