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:
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:
CGImageSourceCreateWithURL:
;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.