【发布时间】:2019-09-26 20:10:02
【问题描述】:
我想使用函数CGImageSourceCreateThumbnailAtIndex 从UIImage 创建缩略图。我所拥有的只是UIImage 本身。图片是UIView 上的快照。
拜托,我不想使用任何其他方法来创建缩略图,只是使用CGImageSourceCreateThumbnailAtIndex 的方法,因为我想将它的性能与我已有的其他方法进行比较。
说,这是我目前的代码。
我使用此代码创建了一个UIImage 类别:
- (UIImage *)createSquaredThumbnailWithWidth:(NSInteger)width {
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(id) kCGImageSourceThumbnailMaxPixelSize : @(width)
};
CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(????, 0, options);
UIImage* scaled = [UIImage imageWithCGImage:scaledImageRef];
CGImageRelease(scaledImageRef);
return scaled;
}
我的问题在于这一行:
CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(????, 0, options);
这个函数的第一个参数需要一个CGImageSourceRef,但就像我说的,我只有一个UIImage,那个图像在内存上,而不是在磁盘上,我不想把它保存到磁盘上,否则性能将付诸东流。
我如何从内存中的UIImage 获取CGImageSourceRef???
【问题讨论】:
-
内存呢?所有图像实例都在内存中。正如@Nirav D 所说的那样使用它。
-
出于好奇,从 UIImage 创建缩略图的哪种方法是“最好的”(在内存使用、时间等方面)?
标签: ios uiimage cgimageref cgimagesource