Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

iphone - How to convert image path to uiimage using ALAssetsLibrary

My iphone application have a array for store the image path selected by user form gallery. I want use ALAssetsLibrary to convert all image path in array to uiimage. How can I do this action? I try used loop, but can not. Thx for the help.

ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:[filePath objectAtIndex:0] resultBlock:^(ALAsset *asset) {
    UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
    [fileImage addObject:image];
} failureBlock:^(NSError *error) {

}];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please use the below code

typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error); ? ?
                ? ? ? ?
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){
                ? ? ? ?
 ALAssetRepresentation *rep = [myasset defaultRepresentation];
 CGImageRef iref = [rep fullResolutionImage];
         ? ?
 if (iref){

         dispatch_async(dispatch_get_main_queue(), ^{
             UIImage *myImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
             [fileImage addObject:myImage];
             //binding ur UI elements in main queue for fast execution
             //self.imageView.image = myImage;
         });

            ?  ? ? ? ? ? ? ? ? ?
         } ? ? ?
}; ? ? ?
                ? ? ? ?
ALAssetsLibraryAccessFailureBlock failureblock ?= ^(NSError *myerror){
                ? ? ? ?
     //failed to get image.
};                   ? ? ? ?
                ? ? ? ?
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:[filePath objectAtIndex:0]?resultBlock:resultblock failureBlock:failureblock];

Note: Make sure that, your [filePath objectAtIndex:0] will be a NSUrl object. Please convert it to NSUrl, if not.

Example:

ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];

NSURL myAssetUrl = [NSURL URLWithString:[filePath objectAtIndex:0]];

assetslibrary assetForURL:myAssetUrl resultBlock:resultblock failureBlock:failureblock];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...