我想按名称从 iphone 中的图库中获取图像.. 就像我在图库 one.png、two.png、three.png 中有 10 张照片等等。我想使用两个.png。我怎样才能得到那个图像。并在我的 UIImageView 中使用它。这可能吗?
问问题
216 次
2 回答
2
imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)
在这个方法中写
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[ImageViewName setImage:image];
[picker1 dismissViewControllerAnimated:YES completion:nil];
于 2012-11-28T12:36:21.020 回答
1
要从照片库中挑选图像,您可以使用UIImagePickerController。
如果你有图片的assetlibrary url,那么你可以使用以下代码:
typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [myasset aspectRatioThumbnail];
UIImage *images;
if (iref)
{
images = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
}
else
{
images = [UIImage imageNamed:@"Noimagey.png"];
}
dispatch_async(dispatch_get_main_queue(), ^{
yourImageView.contentMode = UIViewContentModeScaleAspectFit;
[yourImageView setImage:images];
});
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
UIImage *images = [UIImage imageNamed:@"Nofile.png"];
dispatch_async(dispatch_get_main_queue(), ^{
[yourImageView setImage:images];
});
};
NSURL *asseturl = [NSURL URLWithString:yourAssetUrl];
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:asseturl resultBlock:resultblock failureBlock:failureblock];
如果您使用资产库来挑选图像,请始终记住:
- 这是一个异步过程
- 始终从资产块将图像绑定到主线程中的 imageView
于 2012-11-28T12:49:07.213 回答