I using CGImageSourceCreateThumbnailAtIndex to downsample a huge image, which size is 23622 × 11811. I set the kCGImageSourceThumbnailMaxPixelSize key to 4683. Then the image will be resized to 4683 * 2341, which will take about 43877376.0 bytes when decoded. But when I run to CGImageSourceCreateThumbnailAtIndex, the memory rise about 230MB. This is my code. Is there something wrong?
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)
}
}
}
----------update---------------
I found when using iOS 13. there is no memory spike when CGImageSourceCreateThumbnailAtIndex. I don't know if it's a bug of iOS 12.