2012-08-07 192 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