问题描述
我的主要问题是我需要获取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 loadsthe 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生成自定义缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!