如果您使用的是imagePickerController,委託方法會給你你所需要的
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if ([[info allKeys] containsObject:UIImagePickerControllerReferenceURL]){
// you will get this key if your image comes from a library
[self setMetaDataFromAssetLibrary:info];
} else if ([[info allKeys] containsObject:UIImagePickerControllerMediaMetadata]){
// if the image comes from the camera you get the metadata in it's own key
self.rawMetaData = [self metaDataFromCamera:info];
}
}
從資產庫 - 請記住,它需要時間來完成,並有一個異步完成塊,所以你可能想添加一個完成標誌,以確保你沒有更新之前訪問屬性。
- (void) setMetaDataFromAssetLibrary:(NSDictionary*)info
{
NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
self.rawMetaData = asset.defaultRepresentation.metadata;
}
failureBlock:^(NSError *error) {
NSLog (@"error %@",error);
}];
}
來自相機:
- (NSDictionary*)metaDataFromCamera:(NSDictionary*)info
{
NSMutableDictionary *imageMetadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
return imageMetadata;
}
這裏是如何從一個UIImage
- (NSDictionary*)metaDataFromImage:(UIImage*)image
{
NSData *jpegData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)];
return [self metaDataFromData:jpegData];
}
獲得元數據,但照顧 - 一個UIImage已經可以從原來的剝離的多的元數據的..你會更好地從NSData中獲取用於創建UIImage的元數據...
- (NSDictionary*)metaDataFromData:(NSData*)data
{
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
CFDictionaryRef imageMetaData = CGImageSourceCopyPropertiesAtIndex(source,0,NULL);
return (__bridge NSDictionary *)(imageMetaData);
}
當我嘗試運行[self metaDataFromCamera:info];我得到一個錯誤,該方法無法識別。 (viewController沒有可見的接口聲明選擇器metaDataFromCamera:info)。我需要添加一個庫嗎? – Mrwolfy
@Mrwolfy對不起,我錯過了方法:檢查我的更新回覆。它只是一個單線程:我將它分解爲一個單獨的方法,因爲我正在製作一組元數據讀取和寫入方法。 – foundry