我正在尝试使用 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);
问题是
resizedImage
为空,并且 CGImageSourceGetCount(sourceRef)
返回 0。不过,数据提供程序中确实有相当多的数据,而且这些数据似乎是有效的图像数据。 ALAsset
来自 iPhone 4S 相机胶卷。我错过了什么?为什么
CGImageSourceCreateWithDataProvider()
创建一个包含 0 个图像的图像源?
最佳答案
CGImageSource 用于反序列化序列化图像,例如 JPEG、PNG 等。CGImageGetDataProvider
返回(提供者)图像的原始像素数据。它不会以某种外部格式返回序列化字节。 CGImageSource 无法知道任何给定的原始像素数据所在的像素格式(颜色空间、每组件位、alpha 布局等)。
您可以尝试获取 Assets 代表的 URL 并将其提供给 CGImageSourceCreateWithURL
.如果这不起作用(例如,不是文件 URL),则必须通过 CGImageDestination 运行图像,并在放置输出的任何位置创建 CGImageSource。
(要尝试的另一件事是查看代表的 filename
是否实际上是完整路径,Cocoa 经常误用该术语的方式。但您可能不应该指望这一点。)
关于core-graphics - 如何从 ALAssetRepresentation 创建有效的 CGImageSourceRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17331756/