[英]Getting a URL from (to) a “picked” image, iOS
使用下面的代碼,我可以從圖像中提取元數據(預先添加到我的項目中),並將信息呈現為文本。 這正是我想要做的。 通過URL指向圖像來創建SYMetadata
。 initWithAbsolutePathURL
。 我想用UIImage或者正在加載到UIImage的圖像做同樣的事情。 如何獲取選擇器選擇的圖像的URL? 或者如何從此傳入圖像創建“資產”?
該文檔描述了initWithAsset
。 但是還沒弄明白如何使用它,或者這是否是正確的方法。 任何幫助非常感謝。
NSURL *imageURL = [[NSBundle mainBundle] URLForResource:@"someImage" withExtension:@"jpg"];
SYMetadata *metadata = [[SYMetadata alloc] initWithAbsolutePathURL:imageURL];
[textView setText:[[metadata allMetadatas] description]];
注意 :我嘗試添加類似於此的imageURL = [info valueForKey:@"UIImagePickerControllerReferenceURL"];
,在“pickerDidFinish”方法中,但在將此URL添加到上面的代碼后,元數據為null。
如果您使用的是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已經可以從原始文件中刪除大部分元數據了...你最好從用於創建UIImage的NSData中獲取元數據...
- (NSDictionary*)metaDataFromData:(NSData*)data
{
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
CFDictionaryRef imageMetaData = CGImageSourceCopyPropertiesAtIndex(source,0,NULL);
return (__bridge NSDictionary *)(imageMetaData);
}
如果你有一個ALAsset(在我的示例_detailItem中),你可以用這種方式獲得元數據:
NSDictionary *myMetadata = [[_detailItem defaultRepresentation] metadata];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.