使用 CGImageSourceCreateThumbnailAtIndex 从 UIImage 创建缩略图
Creating a thumbnail from UIImage using CGImageSourceCreateThumbnailAtIndex
我想使用函数 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
???
您是否尝试过使用 CGImageSourceCreateWithData
并将图像数据作为 CFDataRef
像这样传递。
NSData *imageData = UIImagePNGRepresentation(image);
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(id) kCGImageSourceThumbnailMaxPixelSize : @(width)
};
CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
UIImage *scaled = [UIImage imageWithCGImage:scaledImageRef];
CGImageRelease(scaledImageRef);
return scaled;
注意: 如果您有 URL
个图像,则可以使用 CGImageSourceCreateWithURL
.
创建 CGImageSourceRef
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef)(imageFileURL), NULL);
Swift 4码
let imageData = UIImagePNGRepresentation(image)!
let options = [
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceThumbnailMaxPixelSize: 300] as CFDictionary
let source = CGImageSourceCreateWithData(imageData, nil)!
let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options)!
let thumbnail = UIImage(cgImage: imageReference)
Swift 5.1 extension
基于 Ivan 的回答
extension UIImage {
func getThumbnail() -> UIImage? {
guard let imageData = self.pngData() else { return nil }
let options = [
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceThumbnailMaxPixelSize: 300] as CFDictionary
guard let source = CGImageSourceCreateWithData(imageData as CFData, nil) else { return nil }
guard let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options) else { return nil }
return UIImage(cgImage: imageReference)
}
}
我想使用函数 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
???
您是否尝试过使用 CGImageSourceCreateWithData
并将图像数据作为 CFDataRef
像这样传递。
NSData *imageData = UIImagePNGRepresentation(image);
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(id) kCGImageSourceThumbnailMaxPixelSize : @(width)
};
CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
UIImage *scaled = [UIImage imageWithCGImage:scaledImageRef];
CGImageRelease(scaledImageRef);
return scaled;
注意: 如果您有 URL
个图像,则可以使用 CGImageSourceCreateWithURL
.
CGImageSourceRef
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef)(imageFileURL), NULL);
Swift 4码
let imageData = UIImagePNGRepresentation(image)!
let options = [
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceThumbnailMaxPixelSize: 300] as CFDictionary
let source = CGImageSourceCreateWithData(imageData, nil)!
let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options)!
let thumbnail = UIImage(cgImage: imageReference)
Swift 5.1 extension
基于 Ivan 的回答
extension UIImage {
func getThumbnail() -> UIImage? {
guard let imageData = self.pngData() else { return nil }
let options = [
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceThumbnailMaxPixelSize: 300] as CFDictionary
guard let source = CGImageSourceCreateWithData(imageData as CFData, nil) else { return nil }
guard let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options) else { return nil }
return UIImage(cgImage: imageReference)
}
}