如何从ALAssetRepresentation创建有效的CGImageSourceRef?

BJ *_*mer 8 core-graphics alasset alassetslibrary

我正在尝试使用它CGImageSourceCreateThumbnailAtIndex来有效地创建一个调整大小的图像版本.我有一些现有的代码用磁盘上的图像来做这件事,现在我正在尝试使用来自的图像ALAssetsLibrary.

这是我的代码:

ALAsset *asset;
ALAssetRepresentation *representation = [asset defaultRepresentation];
CGImageRef imageRef = [representation fullResolutionImage];

CGDataProviderRef provider = CGImageGetDataProvider(imageRef);
CGImageSourceRef sourceRef = CGImageSourceCreateWithDataProvider(provider, NULL);

NSDictionary *resizeOptions = @{
  kCGImageSourceCreateThumbnailWithTransform : @YES,
  kCGImageSourceCreateThumbnailFromImageAlways : @YES,
  kCGImageSourceThumbnailMaxPixelSize : @(2100) 
};

CGImageRef resizedImage = CGImageSourceCreateThumbnailAtIndex(source, 0, resizeOptions);
Run Code Online (Sandbox Code Playgroud)

问题是它resizedImage是null,并CGImageSourceGetCount(sourceRef)返回0.但数据提供程序确实包含相当多的数据,并且数据看起来确实是有效的图像数据.在ALAsset来自一个iPhone 4S的摄像头卷.

我错过了什么?为什么要CGImageSourceCreateWithDataProvider()创建一个包含0张图像的图像源?

Pet*_*sey 4

CGImageSource 用于反序列化序列化图像,例如 JPEG、PNG 等。

CGImageGetDataProvider返回图像的原始像素数据(的提供者) 。它不返回某种外部格式的序列化字节。CGImageSource 无法知道任何给定的原始像素数据采用什么像素格式(颜色空间、每个组件的位数、Alpha 布局等)。

您可以尝试获取资产代表的 URL 并将其提供给CGImageSourceCreateWithURL. 如果这不起作用(例如,不是文件 URL),您必须通过 CGImageDestination 运行图像,并在放置输出的任何位置创建 CGImageSource。

(要尝试的另一件事是查看代表是否filename实际上是完整路径,Cocoa 经常滥用该术语。但您可能不应该指望这一点。)