2016-03-02 82 views
3

我目前使用AVCaptureStillImageOutput來獲取全分辨率圖片。我還可以使用下面的代碼來獲取EXIF元數據:使用新的PHPhotoLibrary保存exif元數據

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) 
    { 

     CFDictionaryRef metaDict = CMCopyDictionaryOfAttachments(NULL, imageSampleBuffer, kCMAttachmentMode_ShouldPropagate); 
     CFMutableDictionaryRef mutableDict = CFDictionaryCreateMutableCopy(NULL, 0, metaDict); 

     NSLog(@"test attachments %@", mutableDict); 

     // set the dictionary back to the buffer 
     CMSetAttachments(imageSampleBuffer, mutableDict, kCMAttachmentMode_ShouldPropagate); 

     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 

     UIImage *image = [[UIImage alloc] initWithData:imageData];  
     [self.delegate frameReadyToSave:image withExifAttachments: mutableDict]; 
    }]; 

元數據位於該mutableDict變量。現在,我想將這張圖片保存在兩個不同的地方,包括元數據。我想將它保存在應用程序文件夾和照片庫中的磁盤上。現在

,我試圖挽救形象,另一種方法,使用下面的(你看到的圖像變量是一個自定義對象):

NSData* imageData = UIImageJPEGRepresentation(image.image, 1.0f); 

[imageData writeToFile:image.filePath atomically:YES]; 

UIImageWriteToSavedPhotosAlbum(image.image, nil, nil, nil); 

現在,圖像被正確保存,但不包含任何Exif元數據。

從我已閱讀的內容中,我需要使用PHPhotoLibrary來做到這一點,但文檔對此不太感興趣。這是我發現的:

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
    PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image.image];  

} completionHandler:nil]; 

但是,如何保存元數據呢?

回答

1

我會建議你使用的ImageIO來實現這一目標:

-(void)frameReadyToSave:(UIImage*)image withExifAttachments:(NSMutableDictionary*)mutableDict 
{ 
    NSData* imageData = UIImageJPEGRepresentation(image, 1.0f); 
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) imageData, NULL); 
    __block NSURL* tmpURL = [NSURL fileURLWithPath:@"example.jpg"]; //modify to your needs 
    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef) tmpURL, kUTTypeJPEG, 1, NULL); 
    CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef) mutableDict); 
    CGImageDestinationFinalize(destination); 
    CFRelease(source); 
    CFRelease(destination); 
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
     [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:tmpURL]; 
    } completionHandler:^(BOOL success, NSError *error) { 
     //cleanup the tmp file after import, if needed 
    }]; 
} 
+0

這節省的JPEG的EXIF一部分的元數據,但它並沒有在照片中顯示出來(在Mac上)。 –

+0

[PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:tmpURL]應該在導入時讀出照片的EXIF元數據數據,並用必要的數據(記錄日期,位置數據)填充iCloud照片庫數據庫。照片是否以正確的時間戳顯示在iOS照片應用程序中? – holtmann

+0

事實上,它適用於日期和地點。它不適用於標題,描述和關鍵字。至少不是我嘗試過的EXIF字段。 –