Tuesday, 6 August 2013

Inprecise coordinates saved for an images meta data in iOS

Inprecise coordinates saved for an images meta data in iOS

I generate a graph and render it into an image context, I then attempt to
attach this meta data to it so that I can associate a lat long to the
location but the lat long is not precise enough. It's only good for the 3
sometimes 4 numbers after the decimal point, while I need it up to 5-6.
I'll add a test dictionary with a lat long of
double lat = 123.456789;
double lon = 987.654321;
And It'll give back
Latitude = "123.4568333333333";
Longitude = "987.6543333333333";
Because I generate this image from code this cuts out any help from the
asset library, and the coordinates I get are from touching the map, so
that cuts out any imprecision from the GPS since I'm not using it to get
the coordinates, just the map. Is there anyway to make the key save with
greater accuracy?
I'm resorting to saving the string of the lat long in a different key for
the time being but I feel that this is incredibly hacky.
// create meta data
NSDictionary *gpsDictionary = @{(__bridge
NSString*)kCGImagePropertyGPSLatitude : @(site.coordinate.latitude),
(__bridge
NSString*)kCGImagePropertyGPSLongitude:
@(site.coordinate.longitude),
(__bridge
NSString*)kCGImagePropertyGPSLatitudeRef :
site.coordinate.latitude >= 0 ? @"N" :
@"S",
(__bridge
NSString*)kCGImagePropertyGPSLongitudeRef:
site.coordinate.longitude >= 0 ? @"E" :
@"W"};
NSString *userComment = [site description];
NSDictionary *exifDictionary = @{(__bridge
NSString*)kCGImagePropertyExifUserComment : userComment};
NSDictionary *metaData = @{(__bridge NSString
*)kCGImagePropertyGPSDictionary : gpsDictionary,
(__bridge NSString
*)kCGImagePropertyExifDictionary:
exifDictionary};
// save using this format
NSString *fileExtension = (imageFormat == ImageLoaderImageFormatJpeg) ?
@"JPG" : @"PNG";
path = [path stringByReplacingOccurrencesOfString:[path pathExtension]
withString:fileExtension];
// the uti dictates the file format
NSString *uti = (imageFormat == ImageLoaderImageFormatJpeg) ?
@"public.jpeg" : @"public.png";
//this will be the data CGImageDestinationRef will write into
NSMutableData *dest_data = [NSMutableData data];
CGImageDestinationRef destination =
CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,
(__bridge CFStringRef)uti, 1, NULL);
if(!destination) {
NSLog(@"***Could not create image destination ***");
return NO;
}
CGImageDestinationAddImage(destination, imageRef, (__bridge
CFDictionaryRef)metaData);
//tell the destination to write the image data and metadata into our data
object.
//It will return false if something goes wrong
BOOL success = CGImageDestinationFinalize(destination);
if(!success) {
NSLog(@"***Could not create data from image destination ***");
return NO;
}
writeSuccessful = [dest_data writeToFile:path atomically:YES];
I used the following for reference
http://oleb.net/blog/2011/09/accessing-image-properties-without-loading-the-image-into-memory/
Write UIImage along with metadata (EXIF, GPS, TIFF) in iPhone's Photo library

No comments:

Post a Comment