2010-10-01 83 views
66

我正在使用ALAsset框架访问设备的照片库中的文件。从iPhone中的ALAsset中检索的URL显示图像

到目前为止,我可以访问缩略图并显示它。
我想显示图像视图中的实际图像,但我无法弄清楚如何做到这一点。

我尝试使用ALAsset对象中的URLs字段,但未成功。

有人知道如何做到这一点吗?

下面是一些代码,我是能够访问的缩略图,并把它放在一个表格单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    //here 'asset' represents the ALAsset object 


    asset = [assets objectAtIndex:indexPath.row]; 
     //i am accessing the thumbnail here 
    [cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]]; 
    [cell.textLabel setText:[NSString stringWithFormat:@"Photo %d", indexPath.row+1]]; 

    return cell; 
} 
+4

我想出做到这一点的一种方法:\t ALAssetRepresentation * assetRep = [资产defaultRepresentation]; UIImage * image = [UIImage imageWithCGImage:[assetRep fullResolutionImage]]; 但我想通过URL访问它。帮助... – Bangdel 2010-10-01 08:43:08

回答

93

的API有所改变了规则,你没有得到直接的文件系统访问iPhoto图库任何更多。相反,您可以像这样获取资产库网址。

assets-library://asset/asset.JPG?id=1000000003&ext=JPG

您使用ALAssetLibrary对象通过URL来访问ALAsset对象。

所以从文档的ALAssetLibrary抛出这个在报头(或源)

typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset); 
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error); 

这心不是严格必要的,但很让事情。
然后在你的来源。

-(void)findLargeImage 
{ 
    NSString *mediaurl = [self.node valueForKey:kVMMediaURL]; 

    // 
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
    { 
     ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
     CGImageRef iref = [rep fullResolutionImage]; 
     if (iref) { 
      largeimage = [UIImage imageWithCGImage:iref]; 
      [largeimage retain]; 
     } 
    }; 

    // 
    ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) 
    { 
     NSLog(@"booya, cant get image - %@",[myerror localizedDescription]); 
    }; 

    if(mediaurl && [mediaurl length] && ![[mediaurl pathExtension] isEqualToString:AUDIO_EXTENSION]) 
    { 
     [largeimage release]; 
     NSURL *asseturl = [NSURL URLWithString:mediaurl]; 
     ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease]; 
     [assetslibrary assetForURL:asseturl 
         resultBlock:resultblock 
         failureBlock:failureblock]; 
    } 
} 

几件事情需要注意的是,这里使用之前,我开始了我的iOS4移植这是新的给我块,但你可能想看看

https://www.mikeash.com/pyblog/friday-qa-2008-12-26.html

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html

他们弯曲你的头,但如果你认为他们作为通知选择器或回调它有帮助。

而且

  • findLargeImage返回 resultblock不会有作为 回调尚未运行时。所以largeImage不会是 有效。
  • largeImage需要是一个 实例变量,其范围不在 方法中。

我使用这种构造来做到这一点,当使用该方法,但你可能会发现更适合你的使用方法。

[node.view findLargeImage]; 
UIImage *thumb = node.view.largeImage; 
if (thumb) { blah blah } 

那就是我在试图让这个工作时学到的东西。

iOS 5的更新

当结果框大火似乎有点慢iOS5的&也许单核设备,使我不能依靠在图像上调用findLargeImage后直接可用。所以我将它改为致电代表。

@protocol HiresImageDelegate <NSObject> 
@optional 
-(void)hiresImageAvailable:(UIImage *)aimage; 
@end 

和COMMECá

// 
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
    { 
     ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
     CGImageRef iref = [rep fullResolutionImage]; 
     if (iref) { 
      UIImage *largeimage = [UIImage imageWithCGImage:iref]; 
      [delegate hiresImageAvailable:large]; 
     } 
    }; 
+0

你好,我知道这是一个老问题,但希望有人知道。在这个例子中,只有一个图像被设置。我在ALAsset中有多个图像,并且需要将每个图像设置为UIImageView。例如,像这样:largeimage1 = [UIImage imageWithCGImage:iref]; largeimage2 = [UIImage imageWithCGImage:iref]; largeimage3 = [UIImage imageWithCGImage:iref]; ....等。 – mreynol 2014-10-26 23:37:57

+0

我用它在表格视图中显示图像。但它使滚动表格视图变得不稳定和不连贯。滚动在重新生成单元格之前暂停一段时间。我试图在后台线程中做,但看起来不错。请帮忙。 – 2015-10-28 04:58:49

15

沃伦的回答工作很适合我。对于某些人来说,一个有用的东西是同时包含图像方向和缩放元数据。你这样做你的结果框,如下所示:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
{ 
    ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
    CGImageRef iref = [rep fullResolutionImage]; 
    if (iref) 
    { 
     UIImage *largeimage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]]; 
     [delegate hiresImageAvailable:large]; 
    } 
}; 

imageWIthCGImage呼叫这种情况下,具有规模和orientation增加时,它为您创建一个UIImage

[UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]]; 

一个窍门需要注意的是,如果你在iOS 5使用[rep fullScreenImage]而不是[rep fullResolutionImage]你会得到一个已经旋转的图像的 - 但它是在iPhone屏幕的分辨率 - 即其以较低的分辨率。

9

只是为了沃伦和oknox的答案组合成一个较短的片段:

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; 
[assetsLibrary assetForURL:self.selectedPhotos[i] resultBlock: ^(ALAsset *asset){ 
    ALAssetRepresentation *representation = [asset defaultRepresentation]; 
    CGImageRef imageRef = [representation fullResolutionImage]; 
    if (imageRef) { 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     imageView.image = [UIImage imageWithCGImage:imageRef scale:representation.scale orientation:representation.orientation]; 
     // ... 
    } 
} failureBlock: ^{ 
    // Handle failure. 
}]; 

我个人比较喜欢设置我的failureBlocknil

8
 NSURL* aURL = [NSURL URLWithString:@"URL here"]; 

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
    [library assetForURL:aURL resultBlock:^(ALAsset *asset) 
    { 
    UIImage *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp]; 

    cell.backgroundView = [[UIImageView alloc] initWithImage:copyOfOriginalImage]; 
    } 
    failureBlock:^(NSError *error) 
    { 
    // error handling 
    NSLog(@"failure-----"); 
    }]; 

只是提供了UIReferenceURl你得到的图片在上面提供的photolibrary ...它的工作正常。 。 。我diplayed它

  • UIcollectionView细胞

    ..如果你只是想在一个

  • 的UIImageView

    显示它意味着

变化

cell.backgroundView = [[UIImageView alloc] initWithImage:copyOfOriginalImage]; 

imageView.image = copyOfOriginalImage;