当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - iOS - display image from `ALAssetLibrary`

问题描述:

I have ViewController1 and ViewController2.

ViewController1 is UICollectionView and it use ASAssetLibrary to load thumbnail into UICollectionViewCell:

NSMutableArray *collector = [[NSMutableArray alloc] initWithCapacity:0];

ALAssetsLibrary *al = [TemplateEditBarController defaultAssetsLibrary];

[al enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos

usingBlock:^(ALAssetsGroup *group, BOOL *stop)

{

[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)

{

if (asset) {

[collector addObject:asset];

}

}];

assetsarray = collector;//<--assetsarray is local.

}

failureBlock:^(NSError *error) { NSLog(@"fail!");}

];

When cell selected, I want to pass either NSString or NSURL to ViewController2 so that it will call and display the full resolution image. But when NSLog the url:

ALAsset* asset = backgroundsarray[row];

ALAssetRepresentation *representation = [asset defaultRepresentation];

NSURL *url = [representation url];

newbg = [url absoluteString];//<-NSLog this.

I get:

assets-library://asset/asset.JPG?id=6E5438ED-9A8C-4ED0-9DEA-AB2D8F8A9360&ext=JPG

I tried change to [url path] I get:

asset.JPG

How can I get the actual photo url so that I can convert to NSString to pass to ViewController2?

网友答案:
NSURL* aURL = [NSURL URLWithString:imagePath];        
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:aURL resultBlock:^(ALAsset *asset)
     {
         UIImage  *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];             
         imageView.image=image;             

     }
            failureBlock:^(NSError *error)
     {
         // error handling
         NSLog(@"failure-----");
     }];
网友答案:

My solution here:

ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
    if (asset) {
       image = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
    }
}
网友答案:

Apparently there isn't any direct solution other than passing ALAsset. This is what I do:

In ViewController1, which is UICollectionViewController, I have this function:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//NSLog(@"selected row: %ld",(long)indexPath.row);
long row = [indexPath row];
NSString* newbg = [[NSString alloc]init];
if ([bartype isEqualToString:@"photolibrary"]) {
    ALAsset* asset = backgroundsarray[row];
    [_delegate changeBackgroundWithAsset:asset];

}

Then I have a function in ViewController2 to process ALAsset:

-(void)changeBackgroundWithAsset:(ALAsset*)b{
ALAssetRepresentation *rep = [b defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage *bimage;
bimage = [UIImage imageWithCGImage:iref];
UIImageView* bgview = [[UIImageView alloc]init];
bgview = curbgview;
bgview.image = bimage;

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: