【问题标题】:iOS - display image from `ALAssetLibrary`iOS - 显示来自 `ALAssetLibrary` 的图像
【发布时间】:2015-05-13 04:11:49
【问题描述】:

我有ViewController1ViewController2ViewController1UICollectionView,它使用ASAssetLibrary 将缩略图加载到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!");}
 ];

cell selected时,我想通过NSStringNSURLViewController2,使其调用并显示完整的分辨率图像。但是当NSLog这个网址:

ALAsset* asset = backgroundsarray[row];
    ALAssetRepresentation *representation = [asset defaultRepresentation];
    NSURL *url = [representation url];
    newbg = [url absoluteString];//<-NSLog this.

我明白了:

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

我尝试更改为[url path] 我得到了:

资产.JPG

如何获取实际的照片网址,以便我可以转换为NSString 以传递给ViewController2

【问题讨论】:

标签: ios objective-c iphone


【解决方案1】:
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-----");
     }];

【讨论】:

    【解决方案2】:

    我的解决方案:

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

    【讨论】:

    • 但是我不知道为什么加载的图像分辨率不全。
    【解决方案3】:

    显然除了通过ALAsset 之外没有任何直接的解决方案。我就是这样做的:

    ViewController1,也就是UICollectionViewController,我有这个功能:

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

    }

    然后我在ViewController2中有一个函数来处理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;
    

    }

    【讨论】:

      猜你喜欢
      • 2014-08-12
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 2014-04-08
      • 2018-08-05
      • 2011-07-06
      相关资源
      最近更新 更多