UIGraphicsGetImageFromCurrentImageContext memory leak with previews

前端 未结 7 963
悲&欢浪女
悲&欢浪女 2020-11-27 19:19

I\'m trying to create previews images of pages in a PDF but I have some problems with the release of memory.

I wrote a simple test algorithm that cycles on the probl

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 19:57

    For future reference here's what I did to solve this (tested in Swift 4).

    I was calling the function below for every new image downloaded from the internet (on a utility queue). Before implementing the autorelease pool it would crash after processing about 100.

    For simplicity, in the resizeImage function I've removed needed code except for the autoreleasepool and the part that was leaking.

    private func resizeImage(image: UIImage, toHeight: CGFloat) -> UIImage {
        return autoreleasepool { () -> UIImage in
            [...]
    
            let newImage = UIGraphicsGetImageFromCurrentImageContext() //Leaked
            UIGraphicsEndImageContext()
    
            return newImage!
        }
    
    }
    

    I hope this helps!

提交回复
热议问题