从 iPhone 中从 ALAsset 检索的 URL 显示图像 [英] display image from URL retrieved from ALAsset in iPhone

查看:27
本文介绍了从 iPhone 中从 ALAsset 检索的 URL 显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ALAsset 框架来访问设备照片库中的文件.

I am using a ALAsset Framework for accessing the files in the device's photo gallery.

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

So far I am able to access the thumbnail and display it.
I want to display the actual image in an image view but I am unable to figure out how to do this.

我尝试使用 ALAsset 对象中的 URLs 字段,但没有成功.

I tried using the URLs field in the ALAsset object but was unsuccessful.

有人知道这是怎么做到的吗?

Anybody knows how this can be done?

这是我能够访问缩略图并将其放置在表格单元格中的一些代码:

Here's some code where I was able to access the thumbnail and place it in a table cell:

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

推荐答案

API 稍微改变了规则,您不再可以通过文件系统直接访问 iPhoto 库.相反,您会像这样获得资产库 URL.

The API has changed the rules slightly and you dont get direct file system access to the iPhoto library any more. Instead you get asset library URL's like this.

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

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

You use the ALAssetLibrary object to access the ALAsset object via the URL.

因此从 ALAssetLibrary 的文档中将其放入标题(或您的源)

so from the docs for ALAssetLibrary throw this in a header (or your source)

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

这不是严格需要的,但可以保持漂亮.
然后在您的来源中.

which isnt strictly needed but keeps things pretty.
and then in your source.

-(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 之前对我来说是新的块,但您可能想看看

A couple of things to note are that this uses blocks which were new to me before I started my iOS4 porting but you might like to look at

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

它们稍微低下头,但如果您将它们视为通知选择器或回调,那会有所帮助.

They bend your head a little but if you think of them as notification selectors or callbacks it kind of helps.

还有

  • findLargeImage 返回结果块还没有运行,因为它回调.如此大的图像不会是还有效.
  • largeImage 需要是一个实例变量的范围不限于方法.
  • when findLargeImage returns the resultblock wont have run yet as its a callback. So largeImage wont be valid yet.
  • largeImage needs to be an instance variable not scoped to the method.

我在使用该方法时使用此构造来执行此操作,但您可能会发现更适合您使用的内容.

I use this construct to do this when using the method but you may find something more suitable to your use.

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

无论如何,这就是我在尝试使其工作时学到的东西.

Thats what I learned while trying to get this working anyway.

iOS 5 更新

当结果块触发时,iOS5 & 似乎有点慢也许是单核设备,所以我不能依赖于调用 findLargeImage 后直接可用的图像.所以我把它改成呼唤一个代表.

When the result block fires seems to be a bit slower with iOS5 & maybe single core devices so I couldnt rely on the image to be available directly after calling findLargeImage. So I changed it to call out to a delegate.

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

来吧

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

这篇关于从 iPhone 中从 ALAsset 检索的 URL 显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆