我使用CGImageSourceCreateThumbnailAtIndex对一个大小为23622××11811的巨大图像进行下采样。我将kCGImageSourceThumbnailMaxPixelSize键设置为4683。然后,图像将被调整为4683 * 2341,解码后将占用大约43877376.0字节。但是当我运行CGImageSourceCreateThumbnailAtIndex时,内存增加了约230MB。这是我的代码。有什么不对?
private func resizeImageIfNeed(_ url: URL, complete: @escaping (UIImage?, URL) -> Void) {
let destSize = ImageDownsampleUtils.destSize(of: url)
DispatchQueue.global().async {[weak self] in
let sourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
guard let source = CGImageSourceCreateWithURL(url as CFURL, sourceOptions) else {
complete(nil, url)
return
}
let downsampleOptions = [kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceThumbnailMaxPixelSize: destSize,
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true] as CFDictionary
if let downsampledImage = CGImageSourceCreateThumbnailAtIndex(source, 0, downsampleOptions) {
let img = UIImage(cgImage: downsampledImage)
complete(img, url)
} else {
complete(nil, url)
}
}
}
----------更新---------------
我发现使用iOS 13时,CGImageSourceCreateThumbnailAtIndex没有内存高峰。我不知道这是否是iOS 12的错误。