如何使用 ALAssetsLibrary 将图像路径转换为 ​​uiimage [英] How to convert image path to uiimage using ALAssetsLibrary

查看:28
本文介绍了如何使用 ALAssetsLibrary 将图像路径转换为 ​​uiimage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 iphone 应用程序有一个数组,用于存储用户表单库选择的图像路径.我想使用 ALAssetsLibrary 将数组中的所有图像路径转换为 ​​uiimage.我怎样才能做这个动作?我尝试使用循环,但不能.感谢您的帮助.

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) {

}];

推荐答案

请使用下面的代码

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];

注意:请确保您的 [filePath objectAtIndex:0] 将是一个 NSUrl 对象.如果不是,请将其转换为 NSUrl.

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

示例:

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

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

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

这篇关于如何使用 ALAssetsLibrary 将图像路径转换为 ​​uiimage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆