从图像中获取“时间戳”

时间:2012-08-07 10:16:33

标签: objective-c ios

我只是想知道是否有办法获取图片的时间戳,或者该信息是否被写入图像文件,例如从拍摄时开始。

提前致谢

1 个答案:

答案 0 :(得分:6)

导入imageIO框架并尝试使用带时间戳的图像。这是one,将其添加到您的项目中。

#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);