从ALAssetRepresentation生成自定义缩略图 [英] Generating custom thumbnail from ALAssetRepresentation

查看:127
本文介绍了从ALAssetRepresentation生成自定义缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要问题是我需要获取ALAsset对象的缩略图。



我尝试了很多解决方案,并搜索了几天的堆栈溢出,所有的解决方案我由于以下限制,发现对我不起作用:




  • 我无法使用默认缩略图,因为它太少了;

  • 因为屏幕上有很多图像,所以我不能使用fullScreen或fullResolution图像;

  • 我不能使用UIImage或UIImageView来调整大小,因为加载
    的fullResolution图像

  • 我无法将图像加载到内存中,我正在处理20Mpx图像;

  • 我需要创建200x200 px的原始资产版本才能在屏幕上加载;



这是我附带的代码的最后一次迭代:

  #import< AssetsLibrary / ALAsset.h> 
#import< ImageIO / ImageIO.h>

// ...

ALAsset *资产;

// ...

ALAssetRepresentation * assetRepresentation = [asset defaultRepresentation];

NSDictionary * thumbnailOptions = [NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue,kCGImageSourceCreateThumbnailWithTransform,
(id)kCFBooleanTrue,kCGImageSourceCreateThumbnailFromImageAlways,
With(idlo)[NSNumber: ],kCGImageSourceThumbnailMaxPixelSize,
nil];

CGImageRef generateThumbnail = [assetRepresentation CGImageWithOptions:thumbnailOptions];

UIImage * thumbnailImage = [UIImage imageWithCGImage:genicThumbnail];

问题是,生成的 CGImageRef 都不是



我还试图找到一种使用 CGImageSource ,但:




  • 资产网址不能在 CGImageSourceCreateWithURL中使用:;

  • 我无法从 ALAsset ALAssetRepresentation a CGDataProviderRef CGImageSourceCreateWithDataProvider一起使用:;

  • CGImageSourceCreateWithData :要求我将fullResolution或全屏资产存储在内存中才能正常工作。



我失踪了



还有另一种方法可以从 ALAsset ALAssetRepresentation 我不见了?



预先感谢。

解决方案

您可以使用 CGImageSourceCreateThumbnailAtIndex 从可能较大的图像源创建较小的图像。您可以使用 ALAssetRepresentation getBytes:fromOffset:length:error:方法从磁盘加载图像,并使用



然后,您只需传递 kCGImageSourceThumbnailMaxPixelSize kCGImageSourceCreateThumbnailFromImageAlways CGImageSourceCreateThumbnailAtIndex 选项带有已创建的图像源,它将为您创建一个较小的版本,而无需将巨大的版本加载到内存中。 / p>

我写了博客文章要点用这种技术充实了。


My main problem is i need to obtain a thumbnail for an ALAsset object.

I tried a lot of solutions and searched stack overflow for days, all the solutions i found are not working for me due to these constraint:

  • I can't use the default thumbnail because it's too little;
  • I can't use the fullScreen or fullResolution image because i have a lot of images on screen;
  • I can't use UIImage or UIImageView for resizing because those loads the fullResolution image
  • I can't load the image in memory, i'm working with 20Mpx images;
  • I need to create a 200x200 px version of the original asset to load on screen;

this is the last iteration of the code i came with:

#import <AssetsLibrary/ALAsset.h>
#import <ImageIO/ImageIO.h>   

// ...

ALAsset *asset;

// ...

ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation];

NSDictionary *thumbnailOptions = [NSDictionary dictionaryWithObjectsAndKeys:
    (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
    (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
    (id)[NSNumber numberWithFloat:200], kCGImageSourceThumbnailMaxPixelSize,
    nil];

CGImageRef generatedThumbnail = [assetRepresentation CGImageWithOptions:thumbnailOptions];

UIImage *thumbnailImage = [UIImage imageWithCGImage:generatedThumbnail];

problem is, the resulting CGImageRef is neither transformed by orientation, nor of the specified max pixel size;

I also tried to find a way of resizing using CGImageSource, but:

  • the asset url can't be used in the CGImageSourceCreateWithURL:;
  • i can't extract from ALAsset or ALAssetRepresentation a CGDataProviderRef to use with CGImageSourceCreateWithDataProvider:;
  • CGImageSourceCreateWithData: requires me to store the fullResolution or fullscreen asset in memory in order to work.

Am i missing something?

Is there another way of obtaining a custom thumbnail from ALAsset or ALAssetRepresentation that i'm missing?

Thanks in advance.

解决方案

You can use CGImageSourceCreateThumbnailAtIndex to create a small image from a potentially-large image source. You can load your image from disk using the ALAssetRepresentation's getBytes:fromOffset:length:error: method, and use that to create a CGImageSourceRef.

Then you just need to pass the kCGImageSourceThumbnailMaxPixelSize and kCGImageSourceCreateThumbnailFromImageAlways options to CGImageSourceCreateThumbnailAtIndex with the image source you've created, and it will create a smaller version for you without loading the huge version into memory.

I've written a blog post and gist with this technique fleshed out in full.

这篇关于从ALAssetRepresentation生成自定义缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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