从(到)“选择的"获取 URL图像,iOS [英] Getting a URL from (to) a "picked" image, iOS
问题描述
使用下面的代码,我可以从图像中提取元数据(预先添加到我的项目中),并将信息呈现为文本.这正是我想要做的.SYMetadata
是通过 URL 指向图像创建的.initWithAbsolutePathURL
.我想对 UIImage 或者正在加载到 UIImage 的图像做同样的事情.如何获取选择器选择的图像的 URL?或者我如何从这个传入的图像创建一个资产"?
With this code below, I can extract metadata from an image (pre-added to my project), and render the info as text. This is exactly what I want to do. The SYMetadata
is created by pointing to an image via URL. initWithAbsolutePathURL
. I want to do the same thing with a UIImage or maybe the image that is being loaded to the UIImage. How do I get the URL to the image that the picker selects? Or how do I create an "asset" from this incoming image?
文档描述了initWithAsset
.还没有想出如何使用它,或者这是否是达到我目的的正确方法.非常感谢任何帮助.
The documentation describes initWithAsset
. Have not figured out how to use it yet though, or if this is the right way to go for my purpose. Any help greatly appreciated.
NSURL *imageURL = [[NSBundle mainBundle] URLForResource:@"someImage" withExtension:@"jpg"];
SYMetadata *metadata = [[SYMetadata alloc] initWithAbsolutePathURL:imageURL];
[textView setText:[[metadata allMetadatas] description]];
注意:我尝试在pickerDidFinish"方法中添加这样的 NSURL imageURL = [info valueForKey:@"UIImagePickerControllerReferenceURL"];
,但元数据为空在我将这个 URL 添加到上面的代码之后.
Note: I tried adding an NSURL like this imageURL = [info valueForKey:@"UIImagePickerControllerReferenceURL"];
, in the "pickerDidFinish" method but the metadata is null after I add this URL to the above code.
推荐答案
如果你使用的是 imagePickerController,delegate 方法会给你你所需要的
If you are using the imagePickerController, the delegate method will give you what you need
- (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];
}
}
来自资产库 - 请记住,完成需要时间并且具有异步完成块,因此您可能需要添加完成标志以确保在更新之前您不会访问该属性.
From Asset Library - bear in mind that it takes time to complete and has an asynchronous completion block, so you might want to add a completion flag to ensure you don't access the property before it has been updated.
- (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 获取元数据
Here is how to get metadata from a UIImage
- (NSDictionary*)metaDataFromImage:(UIImage*)image
{
NSData *jpegData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)];
return [self metaDataFromData:jpegData];
}
但请注意 - UIImage 已经可以从原始数据中剥离大部分元数据......你最好从用于创建 UIImage 的 NSData 中获取元数据......
But take care - a UIImage can already stripped of much of the metadata from the original.. you will be better off getting the metadata from the NSData that was used to create the UIImage...
- (NSDictionary*)metaDataFromData:(NSData*)data
{
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
CFDictionaryRef imageMetaData = CGImageSourceCopyPropertiesAtIndex(source,0,NULL);
return (__bridge NSDictionary *)(imageMetaData);
}
这篇关于从(到)“选择的"获取 URL图像,iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!