1

I just wondered whether there is a way of getting a timestamp of a picture, or if that info even gets written to an image file, in example from when it was taken.

Thanks in advance

Tom
  • 346
  • 2
  • 18

1 Answers1

6

Import the imageIO framework and try with an image with a timestamp. Here is one, add it to your project.

#import <ImageIO/ImageIO.h>

NSString *path = [[NSBundle mainBundle] pathForResource:@"gg_gps" ofType:@"jpg"];
NSURL *imageFileURL = [NSURL fileURLWithPath:path];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)CFBridgingRetain(imageFileURL), NULL);

// or you can get your imageSource this other way:
// NSData *data =  [NSData dataWithContentsOfFile:path];
// CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache, nil];
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)CFBridgingRetain(options));
CFDictionaryRef exifDic = CFDictionaryGetValue(imageProperties, kCGImagePropertyExifDictionary);
if (exifDic){
    NSString *timestamp = (NSString *)CFBridgingRelease(CFDictionaryGetValue(exifDic, kCGImagePropertyExifDateTimeOriginal));
    if (timestamp){
        NSLog(@"timestamp: %@", timestamp);
    } else {
        NSLog(@"timestamp not found in the exif dic %@", exifDic);
    }
} else {
    NSLog(@"exifDic nil for imageProperties %@",imageProperties);
}
CFRelease(imageProperties);
Jano
  • 62,815
  • 21
  • 164
  • 192
  • Since I already have the image as an object I used only the last two lines of the commented away part of the code. However, xcode gives me an error where it told me to switch (CFDataRef) to (__bridge CFDataRef) and when I did that I got a rumtime error saying: Undefined symbols for architecture i386: *most of your functions from the code*. ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation). What does that mean? – Tom Aug 07 '12 at 20:16
  • You need to add the ImageIO framework to your target. http://stackoverflow.com/questions/3352664/how-to-add-existing-frameworks-in-xcode-4 – rob mayoff Aug 07 '12 at 22:20
  • Sorry I've been slow... But I get: "timestamp not found in the exif dic" on all my iphone pics. Does this mean iphone have as a standard not to create a timestamp when taking a pic? – Tom Aug 10 '12 at 13:13
  • I don't know. I found this: http://itunes.apple.com/us/app/exif-wizard/id387652357?mt=8 it can test your photos, and if you expand the description it will tell you how to enable geotagging in your photos. – Jano Aug 10 '12 at 14:30