views:

187

answers:

2

Hi everyone. A link or a bit of code would be much appreciated!

I have an app that lets users take photos. Here's the code I use to create the jpeg file.

How can I add a geo-tag to the photo's EXIF data, assuming the parameter info has a lat and lon?

- (void) saveImage:(NSDictionary*) info {

  NSFileManager *fileManager = [NSFileManager defaultManager];

  NSString *filePath = [self applicationDocumentsDirectory] 
    stringByAppendingString: @"/photos/"];
  [fileManager createDirectoryAtPath: filePath withIntermediateDirectories: NO attributes: nil error: nil];

  filePath = [filePath stringByAppendingString: [info objectForKey: @"title"]];
  filePath = [filePath stringByAppendingString: @".jpg"];
  [fileManager createFileAtPath:filePath contents:nil attributes:nil];  
  NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

  NSData * imgData = [[NSData alloc] initWithData: 
    UIImageJPEGRepresentation([info objectForKey: @"image"],.8)];

  [fileHandle writeData:imgData];
  [fileHandle closeFile];   
  [imgData release];

}
+2  A: 

iPhone photos are automatically geotagged in the JPEG EXIF

+1  A: 

The iPhone does not currently offer the ImageIO library that can be used for this on the Mac. Take a look at the iphone-exif project, it might provide what you need.

Also, a few notes about your code, if I may.

  • NSString has many path methods that you should use to make your intent more clear when handling paths.
  • UIImageJPEGRepresentation already returns an autoreleased NSData instance. It's a common mistake for beginners, but you are wasting code and adding extra memory management work by creating a new NSData instance with the old one.
  • NSData has a method -writeToFile:options:error: that will let you get rid of all that extra NSFileHandle code. I think it will also save you from having to create the file with NSFileManager first.
  • Alternatively, you could pass the data from UIImageJPEGRepresentation into the contents parameter of -[NSFileManager createFileAtPath:contents:attributes:]
natevw