How to convert image path to uiimage using ALAssetsLibrary

后端 未结 1 443
忘掉有多难
忘掉有多难 2020-12-21 14:55

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

相关标签:
1条回答
  • 2020-12-21 15:21

    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];
    
    0 讨论(0)
提交回复
热议问题