Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 363
akashic Avatar asked Jul 14 '11 20:07

akashic


People also ask

How do I view EXIF data on iPhone?

Here's how to get to the new information pane in the ‌Photos‌ app on iPhones and iPads running ‌iOS 15‌/iPadOS 15. Open the Photos app and tap an image in your Library. Tap the info button (the encircled "i" icon) below the image. Look for the EXIF data in the box below the date and time.

Can you add metadata to iPhone Photos?

Add, edit, and remove other metadata 1) Open the image in the Exif Metadata app. 2) Tap Edit from the top right or scroll to the bottom and tap Edit Exif. 3) From here, tap the individual fields to add new values or edit the current ones. Finally, tap Save.

Does iPhone strip EXIF data?

You can either use Exif Metadata or Photo & Video Metadata Remover to view the Exif data of any photo on your iPhone or iPad. You'll also be able to use these apps to remove the metadata. To view the Exif data of an image, open the Exif Metadata app and it'll ask your permission to access your photos.


1 Answers

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.

like image 158
Anomie Avatar answered Oct 11 '22 22:10

Anomie