[Revision of this answer due to downvotes without explanation.]
Apple updated its article on this issue (Technical Q & A QA1622). If you are using an older version of Xcode, you can still have an article that says more or less complicated luck, you cannot do this without a low-level analysis of image data.
The updated version is here:
https://developer.apple.com/library/ios/#qa/qa1622/_index.html
I adapted the code as follows:
- (void) saveImage:(UIImage *)imageToSave withInfo:(NSDictionary *)info { // Get the image metadata (EXIF & TIFF) NSMutableDictionary * imageMetadata = [[info objectForKey:UIImagePickerControllerMediaMetadata] mutableCopy]; // add (fake) GPS data CLLocationCoordinate2D coordSF = CLLocationCoordinate2DMake(37.732711,-122.45224); // arbitrary altitude and accuracy double altitudeSF = 15.0; double accuracyHorizontal = 1.0; double accuracyVertical = 1.0; NSDate * nowDate = [NSDate date]; // create CLLocation for image CLLocation * loc = [[CLLocation alloc] initWithCoordinate:coordSF altitude:altitudeSF horizontalAccuracy:accuracyHorizontal verticalAccuracy:accuracyVertical timestamp:nowDate]; // this is in case we try to acquire actual location instead of faking it with the code right above if ( loc ) { [imageMetadata setObject:[self gpsDictionaryForLocation:loc] forKey:(NSString*)kCGImagePropertyGPSDictionary]; } // Get the assets library ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; // create a completion block for when we process the image ALAssetsLibraryWriteImageCompletionBlock imageWriteCompletionBlock = ^(NSURL *newURL, NSError *error) { if (error) { NSLog( @"Error writing image with metadata to Photo Library: %@", error ); } else { NSLog( @"Wrote image %@ with metadata %@ to Photo Library",newURL,imageMetadata); } }; // Save the new image to the Camera Roll, using the completion block defined just above [library writeImageToSavedPhotosAlbum:[imageToSave CGImage] metadata:imageMetadata completionBlock:imageWriteCompletionBlock]; }
and I call it from
imagePickerController:didFinishPickingMediaWithInfo:
which is the delegate method for the image picker. (Where I set the logic to see if there is an image to save, etc.)
For completeness, here is a helper method for getting GPS data as a dictionary:
- (NSDictionary *) gpsDictionaryForLocation:(CLLocation *)location { CLLocationDegrees exifLatitude = location.coordinate.latitude; CLLocationDegrees exifLongitude = location.coordinate.longitude; NSString * latRef; NSString * longRef; if (exifLatitude < 0.0) { exifLatitude = exifLatitude * -1.0f; latRef = @"S"; } else { latRef = @"N"; } if (exifLongitude < 0.0) { exifLongitude = exifLongitude * -1.0f; longRef = @"W"; } else { longRef = @"E"; } NSMutableDictionary *locDict = [[NSMutableDictionary alloc] init];
See also Record UIImage along with metadata (EXIF, GPS, TIFF) in the iPhone photo library
Code Roadie Jun 14 2018-12-12T00: 00Z
source share