0

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? enter image description here

    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.

bupo.jung
  • 81
  • 9
  • when dealing with the image always we can use `autoreleasepool`, this will release memory after image operation. – PiyushRathi Aug 29 '19 at 10:56
  • @PiyushRathi In my case, the objects will be released when the runloop ended. And I also try your advice and not work – bupo.jung Aug 29 '19 at 11:33

0 Answers0