iOS - 显示来自`ALAssetLibrary`

时间:2015-05-13 04:11:50

标签: ios objective-c iphone

我有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后,我想将NSStringNSURL传递给ViewController2,以便调用并显示全分辨率图像。但是当NSLog网址:

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

我明白了:

  ?

资产库://asset/asset.JPG ID = 6E5438ED-9A8C-4ED0-9DEA-AB2D8F8A9360&安培; EXT = JPG

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

  

asset.JPG

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

3 个答案:

答案 0 :(得分:2)

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-----");
     }];

答案 1 :(得分:1)

我的解决方案:

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

答案 2 :(得分:0)

显然,除了传递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;

}