Skip to content

Instantly share code, notes, and snippets.

@hjue
Forked from phildow/CLLocation+EXIFGPS.h
Created July 24, 2013 02:07
Show Gist options
  • Select an option

  • Save hjue/6067606 to your computer and use it in GitHub Desktop.

Select an option

Save hjue/6067606 to your computer and use it in GitHub Desktop.

Revisions

  1. @phildow phildow revised this gist Jul 20, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CLLocation+EXIFGPS.m
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ - (NSDictionary*) EXIFMetadataWithHeading:(CLHeading*)heading

    @end

    #pragma -
    #pragma mark -

    @implementation NSDate (EXIFGPS)

  2. @phildow phildow created this gist Jul 20, 2013.
    15 changes: 15 additions & 0 deletions CLLocation+EXIFGPS.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    #import <CoreLocation/CoreLocation.h>
    #import <ImageIO/ImageIO.h>

    @interface CLLocation (EXIFGPS)

    - (NSDictionary*) EXIFMetadataWithHeading:(CLHeading*)heading;

    @end

    @interface NSDate (EXIFGPS)

    - (NSString*) ISODate;
    - (NSString*) ISOTime;

    @end
    61 changes: 61 additions & 0 deletions CLLocation+EXIFGPS.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #import "CLLocation+EXIFGPS.h"

    NSString * const ISODateFormat = @"yyyy-MM-dd";
    NSString * const ISOTimeFormat = @"HH:mm:ss.SSSSSS";

    @implementation CLLocation (EXIFGPS)

    - (NSDictionary*) EXIFMetadataWithHeading:(CLHeading*)heading
    {
    NSMutableDictionary *GPSMetadata = [NSMutableDictionary dictionary];

    NSNumber *altitudeRef = [NSNumber numberWithInteger: self.altitude < 0.0 ? 1 : 0];
    NSString *latitudeRef = self.coordinate.latitude < 0.0 ? @"S" : @"N";
    NSString *longitudeRef = self.coordinate.longitude < 0.0 ? @"W" : @"E";
    NSString *headingRef = @"T"; // T: true direction, M: magnetic

    // GPS metadata

    [GPSMetadata setValue:[NSNumber numberWithDouble:ABS(self.coordinate.latitude)] forKey:(NSString*)kCGImagePropertyGPSLatitude];
    [GPSMetadata setValue:[NSNumber numberWithDouble:ABS(self.coordinate.longitude)] forKey:(NSString*)kCGImagePropertyGPSLongitude];

    [GPSMetadata setValue:latitudeRef forKey:(NSString*)kCGImagePropertyGPSLatitudeRef];
    [GPSMetadata setValue:longitudeRef forKey:(NSString*)kCGImagePropertyGPSLongitudeRef];

    [GPSMetadata setValue:[NSNumber numberWithDouble:ABS(self.altitude)] forKey:(NSString*)kCGImagePropertyGPSAltitude];
    [GPSMetadata setValue:altitudeRef forKey:(NSString*)kCGImagePropertyGPSAltitudeRef];

    [GPSMetadata setValue:[self.timestamp ISOTime] forKey:(NSString*)kCGImagePropertyGPSTimeStamp];
    [GPSMetadata setValue:[self.timestamp ISODate] forKey:(NSString*)kCGImagePropertyGPSDateStamp];

    if (heading) {
    [GPSMetadata setValue:[NSNumber numberWithDouble:heading.trueHeading] forKey:(NSString*)kCGImagePropertyGPSImgDirection];
    [GPSMetadata setValue:headingRef forKey:(NSString*)kCGImagePropertyGPSImgDirectionRef];
    }

    return [GPSMetadata copy];
    }

    @end

    #pragma -

    @implementation NSDate (EXIFGPS)

    - (NSString*) ISODate
    {
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [f setDateFormat:ISODateFormat];
    return [f stringFromDate:self];
    }

    - (NSString*) ISOTime
    {
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [f setDateFormat:ISOTimeFormat];
    return [f stringFromDate:self];
    }

    @end