我的iphone应用程序有一个数组,用于存储用户表单库选择的图像路径.我想使用ALAssetsLibrary将数组中的所有图像路径转换为uiimage.我该怎么做?我尝试使用循环,但不能.谢谢你的帮助.
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) {
}];
Run Code Online (Sandbox Code Playgroud)
请使用以下代码
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];
Run Code Online (Sandbox Code Playgroud)
注意:确保您的[filePath objectAtIndex:0]将是一个NSUrl对象.NSUrl如果没有,请将其转换为.
例:
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
NSURL myAssetUrl = [NSURL URLWithString:[filePath objectAtIndex:0]];
assetslibrary assetForURL:myAssetUrl resultBlock:resultblock failureBlock:failureblock];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6685 次 |
| 最近记录: |