开发者

How to save photo with EXIF(GPS) AND orientation on iPhone?

开发者 https://www.devze.com 2023-03-20 05:10 出处:网络
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 devi

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 orie开发者_如何学编程ntation? 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消