Swift UIGraphicsGetImageFromCurrentImageContext内存泄漏

时间:2017-08-01 15:02:49

标签: ios swift memory-leaks uiimage uigraphicscontext

我有一个集中式方法来调整UIImages的大小:

public extension UIImage {
    public func scaleToSize(size: CGSize) -> UIImage {
        let hasAlpha = true
        let scale: CGFloat = 0.0

        UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
        self.draw(in: CGRect(origin: CGPoint.zero, size: size))

        let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return scaledImage ?? self
    }
}

问题是UIGraphicsGetImageFromCurrentImageContext()会产生泄漏,因为它会返回autorelease UIImage。因此,当我想将此UIImage分配给UIImageView时,我需要使用autoreleasepool {...}包装分配。问题是我在我的应用程序中有{2000}的scaleToSize(size:)来电...有没有其他方法可以解决这个问题?

谢谢!

0 个答案:

没有答案