Swift UIGraphicsGetImageFromCurrentImageContext memory leak

I have a centralized resize method 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
    }
}

      

The problem is what UIGraphicsGetImageFromCurrentImageContext()

creates leaks because it returns autorelease UIImage

. Hence, when I want to assign this one UIImage

to UIImageView

, I need to associate the assignment with autoreleasepool {...}

. The problem is I have 2000+ calls scaleToSize(size:)

in my application ... is there any other way to fix this?

Thank!

+3
ios memory-leaks swift uiimage uigraphicscontext


source to share


No one has answered this question yet

See similar questions:

13
UIGraphicsGetImageFromCurrentImageContext memory leak with preview

or similar:

3044
Making a memory leak with Java
1146
performSelector may leak because its selector is unknown
1042
The result was a leak of the window that was originally added
928
How do I call Objective-C code from Swift?
902
# pragma in Swift?
870
Swift Beta: Sorting Arrays
641
Split string into array in Swift?
376
Upload / download image from url in Swift
15
swift UIGraphicsGetImageFromCurrentImageContext cannot free memory
3
Modern technology for adding a gradient to UIView



All Articles
Loading...
X
Show
Funny
Dev
Pics