2012-07-30 39 views
2

我的iPhone应用程序有一个数组用于存储用户窗体库选择的图像路径。 我想使用ALAssetsLibrary将阵列中的所有图像路径转换为uiimage。 我该怎么做这个动作?我尝试使用循环,但不能。 Thx寻求帮助。如何使用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) { 

}]; 
+0

可以表明,要存储在阵列中的路径.. – Leena 2012-07-30 13:16:06

+0

资产库://asset/asset.JPG ID = 1000000192&EXT = JPG文件路径(阵列)是存储用户选择的iPhone内置图库的图像路径。 Thx寻求帮助。 – user1562536 2012-07-31 09:54:32

回答

6

请使用下面的代码

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

注:确保,你[文件路径objectAtIndex:0]将是一个NSUrl对象。请将其转换为NSUrl,如果不是。

实施例:

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

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

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

如何使用for循环将filePath(数组)中的所有图像路径转换为使用ALAssetsLibrary的uiimage。我不熟悉块。 Thx寻求帮助。在执行“assetslibrary assetForURL:myAssetUrl resultBlock:resultblock failureBlock:failureblock”行之后的 – user1562536 2012-07-31 04:28:19

+0

,结果块将在另一个线程中被调用。您将在“myImage”对象中获得相应的url图像。将该图像添加到您的数组中。你可以检查数组的数量,以确定你是否得到所有的图像(行[“fileImage addObject:myImage];” – 2012-07-31 07:43:42

+0

当这些代码运行在iPhone 4.3模拟器,它是成功的获取图像,但运行在iPhone 3GS 4.3 .3(真实设备),它没有得到图像,它将进入“failureblock”。filePath(阵列)是存储图像路径的iPhone内置图库,由用户选择。Thx的帮助。 – user1562536 2012-07-31 09:51:48

相关问题