Iphone 使用UIImagePickerControllerReferenceURL在UIImageView上设置图像
是否可以使用在UIImageView上设置图像 UIImagePickerController参考URL。传统的模式是使用 [info objectForKey:@“UIImagePickerController原始图像”] theImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[info objectForKey:UIImagePickerControllerReferenceURL]] 但UIImageView上未设置图像Iphone 使用UIImagePickerControllerReferenceURL在UIImageView上设置图像,iphone,uiimageview,Iphone,Uiimageview,是否可以使用在UIImageView上设置图像 UIImagePickerController参考URL。传统的模式是使用 [info objectForKey:@“UIImagePickerController原始图像”] theImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[info objectForKey:UIImagePickerControllerReferenceURL]] 但UIImage
请帮忙 您需要在代码中进行更多的错误处理,而不是在一行中嵌入四个或五个函数 请考虑以下情况:
NSURL * referenceURL = [info objectForKey: UIImagePickerControllerReferenceURL];
if(referenceURL == NULL)
{
NSLog( @"reference URL is null");
} else {
NSData * imageData = [NSData dataWithContentsOfURL: referenceURL];
if(imageData == NULL)
{
NSLog( @"no image data found for URL %@", [referenceURL absoluteString] );
} else {
UIImage * theActualImage = [UIImage imageWithData: imageData];
if(theActualImage == NULL)
{
NSLog( @"the data at URL %@ is not an image", [referenceURL absoluteString] );
} else {
if(theImageView == NULL)
{
NSLog( @"I forgot to set theImageView" );
} else {
theImageView.image = theActualImage;
}
}
}
}
我希望这些信息能帮助你
NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:referenceURL resultBlock:^(ALAsset *asset)
{
UIImage *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
theImageView.image = copyOfOriginalImage;
}
failureBlock:^(NSError *error)
{
// error handling
}];
[library release];
这段代码成功了。:) 我在控制台中得到了这个,没有找到URL资产的图像数据-library://asset/asset.JPG?id=79450962-C0FD-484C-808B-83E045F40972&ext=JPG,但我从照片库中选择了一幅图像。有什么问题吗?我很高兴你能找出问题出在哪里。现在看来,您需要弄清楚如何将该引用URL转换为可以实际加载到NSData对象中的内容。也许
info
NSDictionary中还有另一个键可以帮助您?:-)但URL似乎是有效的:(那为什么没有图像数据呢?你必须使用AlassetLibrary
来访问这种类型的图像数据。请查找包含AlassetLibraryAssetForUrlResultBlock
的示例代码。抱歉@Michael Dautermann在网上没有找到任何有用的内容。我是iphone新手。你能给我一些建议吗?如果有人能帮忙的话,会很有帮助的。)e可以将其转换为Swift 2.0
NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:referenceURL resultBlock:^(ALAsset *asset)
{
UIImage *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
theImageView.image = copyOfOriginalImage;
}
failureBlock:^(NSError *error)
{
// error handling
}];
[library release];