Deprecation of the UIGraphicsBeginImageContextWithOptions
π Deprecated API Usage: UIGraphicsBeginImageContextWithOptions may cause crash in low-memory
Hi, I noticed that this library is using the deprecated UIGraphicsBeginImageContextWithOptions API in the following line:
https://github.com/SDWebImage/SDWebImageSwiftUI/blob/ea241055c576d977c607d8cac5fac840c90f80f4/SDWebImageSwiftUI/Classes/WebImage.swift#L211-L214
Apple has marked this API as deprecated (with version 100000) and recommends replacing it with UIGraphicsImageRenderer.
π₯ Crash Context (low-memory device)
In some cases, especially on devices with low memory, the following crash can occur:
Fatal Exception: NSInternalInconsistencyException
UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={...}, scale=..., bitmapInfo=...
Use UIGraphicsImageRenderer to avoid this assert.
π Deprecation Info
@available(iOS, introduced: 4.0, deprecated: 100000, message: "Replace usage of UIGraphicsBeginImageContextWithOptions with UIGraphicsImageRenderer.")
public func UIGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat)
@available(iOS, introduced: 2.0, deprecated: 100000, message: "Replace usage of UIGraphicsBeginImageContext with UIGraphicsImageRenderer.")
public func UIGraphicsBeginImageContext(_ size: CGSize)
@available(iOS, introduced: 2.0, deprecated: 100000, message: "Replace usage of UIGraphicsGetImageFromCurrentImageContext with UIGraphicsImageRendererContext.currentImage.")
public func UIGraphicsGetImageFromCurrentImageContext() -> UIImage?
@available(iOS, introduced: 2.0, deprecated: 100000, message: "UIGraphicsEndImageContext should only be used alongside UIGraphicsBeginImageContext[WithOptions].")
public func UIGraphicsEndImageContext()
β Kingfisher Reference
For reference, this has already been fixed in Kingfisher in the following PR:
π https://github.com/onevcat/Kingfisher/pull/2274
π‘ Suggestion
Would you consider updating SDWebImageSwiftUI to use UIGraphicsImageRenderer instead?
Itβs safer and preferred in modern iOS development, and avoids low-memory crashes.
Thanks for your great work on this library! π
Need to drop iOS 9-10. Currently it still support these firmware version
And, actually, you should not hit this issue. Because when running on iOS 11, we use SDGraphicsImageRenderer, which always use UIGraphicsImageRenderer, during the standard image loading pipeline.
unless you do something utils API call by yourself
OK. I notice this is not SDWebImage repo, but SDWebImage SwiftUI port.
This can be polished to use SDGraphicsImageRenderer or CGContext instead
This is used for SVG offscreen rendering. It convert Vector into Bitmap image.
I tested UIGraphicsImageRenderer and it has worse performance for this task. So I don't think you should just replace it because of Deprecation from Apple