SDWebImageSwiftUI icon indicating copy to clipboard operation
SDWebImageSwiftUI copied to clipboard

Deprecation of the UIGraphicsBeginImageContextWithOptions

Open eunjiha1 opened this issue 6 months ago β€’ 3 comments

🐞 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! πŸ™

eunjiha1 avatar Jul 01 '25 01:07 eunjiha1

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

dreampiggy avatar Jul 01 '25 02:07 dreampiggy

OK. I notice this is not SDWebImage repo, but SDWebImage SwiftUI port.

This can be polished to use SDGraphicsImageRenderer or CGContext instead

dreampiggy avatar Jul 01 '25 02:07 dreampiggy

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

dreampiggy avatar Jul 03 '25 03:07 dreampiggy