首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS --“`ALAssetLibrary`”中的显示图像

iOS --“`ALAssetLibrary`”中的显示图像
EN

Stack Overflow用户
提问于 2015-05-13 04:11:49
回答 3查看 689关注 0票数 1

我有ViewController1ViewController2ViewController1UICollectionView,它使用ASAssetLibrary将缩略图加载到UICollectionViewCell中。

代码语言:javascript
复制
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选择时,我希望将NSStringNSURL传递给ViewController2,以便它将调用并显示完整的分辨率图像。但是当NSLog网址:

代码语言:javascript
复制
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],我得到:

asset.JPG

如何获得实际的照片url,以便将其转换为NSString以传递到ViewController2

EN

回答 3

Stack Overflow用户

发布于 2015-05-13 05:07:08

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-10-18 04:38:22

我的解决办法是:

代码语言:javascript
复制
ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
    if (asset) {
       image = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
    }
}
票数 1
EN

Stack Overflow用户

发布于 2015-05-13 13:01:40

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

ViewController1 (也就是UICollectionViewController )中,我有一个函数:

代码语言:javascript
复制
- (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的函数。

代码语言:javascript
复制
-(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;

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30205554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档