如何显示来自PhotoLibrary的特定图像? [英] How to show the specific image from PhotoLibrary?
问题描述
在我的应用程序中,我需要使用 didFinishPickingMediaWithInfo 从照片库中获取特定图片
In my Application I need to get specific image from Photo Library,
获取Imagename和ImagePath。
with use of didFinishPickingMediaWithInfo i am able to get the Imagename and ImagePath.
我在数据库中存储Imagename,ImagePath。
I am storing Imagename, ImagePath in Database.
但是,如何使用
推荐答案
请使用 ALAssetLibrary
。为此,请将 AssetsLibrary.framework
添加到您的项目中,并编写以下代码。
Please make use of ALAssetLibrary
. To do this, add the AssetsLibrary.framework
to your project and write the below code.
看起来像 assets-library://asset/asset.JPG?id = 1000000194& ext = JPG
NSString *fileName = @"assets-library://asset/asset.JPG?id=1000000194&ext=JPG";
typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage *images = nil;
if (iref)
{
images = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
//doing UI operation on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
yourImageView.image = images;
});
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"can't get image");
};
NSURL *asseturl = [NSURL URLWithString:fileName];
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:asseturl
resultBlock:resultblock
failureBlock:failureblock];
注意:
升级到iOS 5并进行代码重构以使用ARC之后,您会收到一条错误:
After the upgrade to iOS 5 and a code refactoring to make use of ARC, you will get an error as
访问ALAssetPrivate超过其
拥有ALAssetsLibraryrefactoring的生命周期以使用ARC
invalid attempt to access ALAssetPrivate past the lifetime of its owning ALAssetsLibraryrefactoring to make use of ARC
要解决此问题,请添加静态方法检索 ALAssetLibrary
类的共享实例。
To resolve this, add a static method to retrieve a shared instance of ALAssetLibrary
class.
+ (ALAssetsLibrary *)defaultAssetsLibrary {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
然后,使用 [MyClass defaultAssetsLibrary]
[[MyClass defaultAssetsLibrary] assetForURL:asseturl
resultBlock:resultblock
failureBlock:failureblock];
这篇关于如何显示来自PhotoLibrary的特定图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!