Last active
December 11, 2015 00:39
-
-
Save yageek/4518092 to your computer and use it in GitHub Desktop.
Edit JPEG Tag of a JPEG file using Core foundation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CGImageSourceRef originalImageSource = CGImageSourceCreateWithURL((CFURLRef)imageURL, NULL); | |
//get gps metadata | |
NSDictionary *metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(originalImageSource,0,NULL); | |
//make the metadata dictionary mutable so we can add properties to it | |
NSMutableDictionary *metadataAsMutable = [[metadata mutableCopy]autorelease]; | |
[metadata release]; | |
//get mutable gps data | |
NSMutableDictionary *GPSDictionary = [[[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGPSDictionary]mutableCopy]autorelease]; | |
if(!GPSDictionary) | |
{ | |
GPSDictionary = [NSMutableDictionary dictionary]; | |
} | |
//Upate latitude longitude | |
// latRef and lonRef are NSString | |
[GPSDictionary setObject:[NSNumber numberWithFloat:lat] forKey:(NSString*)kCGImagePropertyGPSLatitude]; | |
[GPSDictionary setObject:latRef forKey:(NSString*)kCGImagePropertyGPSLatitudeRef]; | |
[GPSDictionary setObject:[NSNumber numberWithFloat:lon] forKey:(NSString*)kCGImagePropertyGPSLongitude]; | |
[GPSDictionary setObject:lonRef forKey:(NSString*)kCGImagePropertyGPSLongitudeRef]; | |
//add our modified GPS data back into the image's metadata | |
[metadataAsMutable setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary]; | |
NSLog(@"Updated metadata: %@", metadataAsMutable); | |
// Create a new image source that reads the TIFF data that we get from our NSImage. | |
NSData *imageData = [newImage TIFFRepresentation]; | |
CGImageSourceRef finalImageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL); | |
// Create an image destination. This will take the image data from our source, and write it along with the metadata we read above | |
// into a file in the correct format. | |
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithURL((CFURLRef)imageURL, imageType, 1, NULL); | |
CGImageDestinationAddImageFromSource(imageDestination, finalImageSource, 0, (CFDictionaryRef)metadataAsMutable); | |
if (!CGImageDestinationFinalize(imageDestination)) | |
{ | |
// The sample code doesn't do any specific error handling in this case. This would be an appropriate | |
// place to notify the user, put up an alert, etc. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment