2

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

}];
user1562536
  • 23
  • 1
  • 3
  • can you show the path that you are storing in the array.. – Leena Jul 30 '12 at 13:16
  • assets-library://asset/asset.JPG?id=1000000192&ext=JPG The filePath(array) is store image path form iPhone built-in gallery selected by user. Thx for the help. – user1562536 Jul 31 '12 at 09:54

1 Answers1

6

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];
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • How can I use for loop to convert all image path in filePath(array) to uiimage with ALAssetsLibrary. I am not familiar with blocks. Thx for the help. – user1562536 Jul 31 '12 at 04:28
  • after the line "assetslibrary assetForURL:myAssetUrl resultBlock:resultblock failureBlock:failureblock]" will executed , the resultblock will get called in another thread. you will get the curresponding url image in "myImage" object.add that image in to your array. you can check the array count to identify if you got all the images (after the line "[fileImage addObject:myImage];" – Shamsudheen TK Jul 31 '12 at 07:43
  • When these code running in iPhone 4.3 Simulator, it is success to get image, but running in iPhone 3GS 4.3.3 (real device), it is failed to get image, it will go to "failureblock". The filePath(array) is store image path form iPhone built-in gallery selected by user. Thx for the help. – user1562536 Jul 31 '12 at 09:51
  • mail your sample application to shamsutk87@gmail.com – Shamsudheen TK Jul 31 '12 at 11:23
  • I find the reason, I am not open the location services. Do you know why using assetslibrary need open the location services? – user1562536 Aug 01 '12 at 01:39