从图像中获取"时间戳"

Tom*_*Tom 1 objective-c ios

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

提前致谢

Jan*_*ano 6

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

#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);
Run Code Online (Sandbox Code Playgroud)