我正在尝试使用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张图像的图像源?
答案 0 :(得分:4)
CGImageSource用于反序列化序列化图像,例如JPEG,PNG等等。
CGImageGetDataProvider
返回图像的原始像素数据的(提供者)。它不会以某种外部格式返回序列化字节。 CGImageSource无法知道任何给定原始像素数据的像素格式(颜色空间,每个组件的位数,alpha布局等)。
您可以尝试获取资产代表的网址并将其提供给CGImageSourceCreateWithURL
。如果这不起作用(例如,不是文件URL),则必须通过CGImageDestination运行图像,并在输出输出的任何位置创建CGImageSource。
(另外要尝试的另一件事是看看代表的filename
是否真的是一条完整的路径,就像Cocoa经常滥用这个词一样。但你可能不应该依赖它。)
答案 1 :(得分:2)
您可以尝试的一件事是the asset rep's CGImageWithOptions:
method。
文档声明:
此方法以任何方式返回可用的最大,最佳表示形式。
但它也说约fullResolutionImage
,而且我不确定为什么这个类如果它们都做同样的事情会有两种方法。我想知道这是否是一个复制粘贴错误。
使用一堆缩略图创建选项尝试CGImageWithOptions:
,看看会发生什么。
答案 2 :(得分:1)
选项#3将是代表fullScreenImage
。根据您需要的“缩略图”类型,使用它可能更便宜和/或更简单,这将不会大于(大约)设备屏幕的大小。
答案 3 :(得分:0)
这也可以帮助......
ALAssetRepresentation* rep = [asset defaultRepresentation];
NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys:
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageAlways,
(id)[NSNumber numberWithDouble:400], (id)kCGImageSourceThumbnailMaxPixelSize,
nil];
CGImageRef image = [rep CGImageWithOptions:options];