2013-02-02 25 views
1

通過下面的代碼,我可以從圖像中提取元數據(預先添加到我的項目中),並將信息呈現爲文本。這正是我想要做的。通過URL指向圖像創建SYMetadatainitWithAbsolutePathURL。我想用UIImage或者正在加載到UIImage的圖像做同樣的事情。如何獲取選取器選擇的圖像的URL?或者,如何從此傳入圖像創建「資產」?通過下面的代碼獲取一個URL到「挑選」的圖像,iOS

該文件描述了initWithAsset。還沒有弄清楚如何使用它,或者如果這是爲我的目的而行的正確方法。任何幫助不勝感激。

NSURL *imageURL = [[NSBundle mainBundle] URLForResource:@"someImage" withExtension:@"jpg"]; 
SYMetadata *metadata = [[SYMetadata alloc] initWithAbsolutePathURL:imageURL]; 
[textView setText:[[metadata allMetadatas] description]]; 

注意:我嘗試添加一個NSURL這樣imageURL = [info valueForKey:@"UIImagePickerControllerReferenceURL"];,在「pickerDidFinish」的方法,但元數據爲空後,我這個URL添加到上面的代碼。

回答

2

如果您使用的是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); 
} 
+0

當我嘗試運行[self metaDataFromCamera:info];我得到一個錯誤,該方法無法識別。 (viewController沒有可見的接口聲明選擇器metaDataFromCamera:info)。我需要添加一個庫嗎? – Mrwolfy

+0

@Mrwolfy對不起,我錯過了方法:檢查我的更新回覆。它只是一個單線程:我將它分解爲一個單獨的方法,因爲我正在製作一組元數據讀取和寫入方法。 – foundry

1

如果您已經在ALAsset(在我的樣本_detailItem),你可以有元數據是這樣的:

NSDictionary *myMetadata = [[_detailItem defaultRepresentation] metadata]; 
+0

請解釋如何獲得ALAsset,但尚未理解概念。謝謝!! – Mrwolfy

+0

使用ALAssetsLibrary的一個很好的例子是:http://developer.apple.com/library/ios/#samplecode/MyImagePicker/Introduction/Intro.html – asclepix