2012-08-07 198 views
1

我只是想知道是否有获取图片时间戳的方法,或者如果该信息甚至被写入图像文件,例如从何时拍摄。从图像获取“时间戳”

在此先感谢

回答

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); 
+0

由于我已经有图像作为一个对象,我只用了注释掉的部分代码的最后两行。 然而,Xcode中给我一个错误的地方告诉我(CFDataRef)切换到(__bridge CFDataRef),当我这样做,我得到了一个rumtime错误说: 为i386硬件架构未定义的符号: *大多数功能从代码*。 ld:未找到架构的符号i386 clang:错误:链接器命令失败,退出代码1(使用-v查看调用)。 这是什么意思? – Tom 2012-08-07 20:16:14

+0

您需要将ImageIO框架添加到您的目标。 http://stackoverflow.com/questions/3352664/how-to-add-existing-frameworks-in-xcode-4 – 2012-08-07 22:20:47

+0

对不起,我一直很慢...但我得到:“在时间戳中找不到时间戳”在我所有的iphone照片上。这是否意味着iphone有一个标准,在拍照时不会创建时间戳? – Tom 2012-08-10 13:13:13