Skip to content

Deprecation of the UIGraphicsBeginImageContextWithOptions #355

Open
@eunjiha1

Description

@eunjiha1

🐞 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:

UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
image.draw(at: .zero)
cgImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage
UIGraphicsEndImageContext()

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:

🔗 onevcat/Kingfisher#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! 🙏

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions