2012-04-09 47 views
0

我正在創建一個圖像處理應用程序,它可以執行兩種圖像分析功能。一種是讀取圖像的RGB數據,另一種是讀取EXIF數據。我正在使用前置攝像頭拍照,然後將其保存到文檔文件夾中。爲了獲取RGB值,我以這種方式加載圖像:iPhone EXIF數據不會從文檔文件夾中讀取

NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"]; 
UIImage *image = [UIImage imageWithContentsOfFile:jpgPath]; 
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage)); 
const UInt8* data = CFDataGetBytePtr(pixelData); 

這工作正常,我可以得到像素數據。我的問題是收集EXIF數據。我以與RGB相同的方式實現讀取圖像,並且所有EXIF數據都返回爲NULL。

NSString *EXIFPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"]; 
NSURL *url = [NSURL fileURLWithPath:EXIFPath]; 

CGImageSourceRef sourceRef = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL); 

NSDictionary *immutableMetadata = (__bridge NSDictionary *) CGImageSourceCopyPropertiesAtIndex(sourceRef,0,NULL); 
NSDictionary *exifDic = [immutableMetadata objectForKey:(NSString *)kCGImagePropertyExifDictionary]; 

NSNumber *ExifApertureValue = [exifDic objectForKey:(NSString*)kCGImagePropertyExifApertureValue]; 
NSNumber *ExifShutterSpeed = [exifDic objectForKey:(NSString*)kCGImagePropertyExifShutterSpeedValue]; 

NSLog(@"ExifApertureValue : %@ \n",ExifApertureValue); 
NSLog(@"ExifShutterSpeed : %@ \n",ExifShutterSpeed); 

如果我改變的第一行代碼在這樣的應用讀取預加載圖像:

NSString *aPath = [[NSBundle mainBundle] pathForResource:@"IMG_1406" ofType:@"JPG"]; 

它的工作原理。問題是我無法預載圖像。他們必須從相機中帶電。任何建議,非常感謝。謝謝。

+0

從這裏 http://stackoverflow.com/questions/1238838/uiimagepickercontroller-and-獲取答案提取exif數據從現有的照片 – 2012-04-09 07:26:18

+0

請參閱http://stackoverflow.com/questions/9766394/get-exif-data-from-uiimage-uiimagepickercontroller/9890442#9890442 – 2012-04-09 07:38:19

+0

我有代碼將讀取EXIF數據,而不是當圖像在手機的Document文件夾中時。有沒有辦法解決它如何讀取圖像而不是重寫所有的EXIF代碼? – tguidon 2012-04-09 15:32:07

回答

1

該文件Test.jpg如何被寫入文檔目錄?它是用UIImageJPEGRepresentation寫的嗎?如果是這樣,EXIF數據將會丟失。請確保您存儲了您需要元數據的任何圖像的JPEG源代碼。

無論發生什麼事情,只要您檢索完整的對象,它將幫助您記錄完整的immutableMetadataexifDic對象。

NSDictionary *immutableMetadata = (__bridge NSDictionary *) CGImageSourceCopyPropertiesAtIndex(sourceRef,0,NULL); 
NSLog(@"immutableMetadata = %@", immutableMetadata); 
NSDictionary *exifDic = [immutableMetadata objectForKey:(NSString *)kCGImagePropertyExifDictionary]; 
NSLog(@"exifDic"); 

如果您exifDic日誌只包含這三個值,它被保存由不關心保留EXIF頭的功能。

的工作,但可能會更好
exifDic = { 
    ColorSpace = 1; 
    PixelXDimension = 1200; 
    PixelYDimension = 1600; 
} 

兩個其他的東西:

(1)有沒有保證的文件目錄將是NSHomeDirectory的子目錄()。可靠的方式來獲得這個文件的位置如下:

NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentDirectory = [documentDirectories objectAtIndex:0]; 
NSString *imagePath = [documentDirectory stringByAppendingPathComponent:@"Test.jpg"]; 

(2)你當前正在加載從存儲圖像的字節數兩次,第一次拿到像素,一旦獲得的元數據。將它們加載到一個NSData對象中,並且只需要檢索一次該文件。保持該對象,並且可以稍後保存圖像而不會丟失任何細節。 (這將消耗內存等於文件的大小,所以只保留它,如果你需要它。)

NSData *imageData = [NSData dataWithContentsOfFile:imagePath]; 
UIImage *image = [UIImage imageWithData:imageData]; 
// Do things involving image pixels... 

CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef) imageData, NULL); 
// Do things involving image metadata...