iphone - 在 iPhone 中显示从 ALAsset 检索到的 URL 中的图像

标签 iphone image url

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

到目前为止,我可以访问缩略图并显示它。
我想在 ImageView 中显示实际图像,但我不知道如何做到这一点。

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

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

下面是一些代码,我可以在其中访问缩略图并将其放置在表格单元格中:

- (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。

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

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

因此,从 ALAssetLibrary 的文档中,将其放入 header (或您的源代码)中

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

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 需要是 实例变量的作用域不限于 方法。

我在使用该方法时使用此构造来执行此操作,但您可能会找到更适合您使用的东西。

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

这就是我在尝试使其正常工作时学到的东西。

iOS 5 更新

当 iOS5 和单核设备上结果 block 触发时似乎有点慢,因此我不能依赖在调用 findLargeImage 后直接使用图像。所以我将其更改为调用代表。

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

然后comme cá

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

关于iphone - 在 iPhone 中显示从 ALAsset 检索到的 URL 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3837115/

相关文章:

html - 响应式设计不适用于 linux cpanel (GoDaddy) - 适用于 classis linux 主机 (GoDaddy) 和本地

java - 我应该在哪里存储图像(和其他文件)以供我的 Java 程序使用?

ruby-on-rails - Rails 获取 url_for 在模型内部工作

iphone - 使用我的应用程序打开文件

iphone - UITextField 返回键文本从 "Done"更改为 "Post"

ios - 调用中缺少参数 ‘coder’ 的参数

php - .htaccess 不工作

iphone - 以编程方式向 View 添加多个按钮,调用相同的方法,确定它是哪个按钮

java - 如何在 Java 中从二维 float 组编写 TIFF?

java - Webview 不断重新加载同一页面