core-graphics - 如何从 ALAssetRepresentation 创建有效的 CGImageSourceRef?

标签 core-graphics alassetslibrary alasset

我正在尝试使用 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/

相关文章:

ios - 相机胶卷图像的创建日期

ios - MPMoviePlayerController:不播放视频

iphone - 在 MFMailComposeViewController 中附加多张照片的内存泄漏

iphone - 给定一个 CGPath,如何让它弯曲?

ios - 如何绘制水平条形图而不显示轴

iphone - 无法从异步 block 向主线程发送参数

objective-c - 是否可以从 NSData 或其他图像类型(CFImageRef、CIImage、UIImage)创建 ALAsset 对象?

iphone - 等待 assetForURL block 完成

带边框的 iOS UIImage 有一个粗角半径

iphone - AVFoundation - 从 Y 平面获取灰度图像 (​​kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)