From 8cd2a27e883ae9458432c5a07eb68a0048844c83 Mon Sep 17 00:00:00 2001 From: Soner Yuksel Date: Tue, 17 Oct 2023 10:58:41 -0400 Subject: [PATCH 1/2] Adding Temporary if check to take care UIGraphicsBeginImageContextWithOptions zero size error --- Sources/Brave/Extensions/UIViewExtensions.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/Brave/Extensions/UIViewExtensions.swift b/Sources/Brave/Extensions/UIViewExtensions.swift index e1ad9f27486..c54045a4a36 100644 --- a/Sources/Brave/Extensions/UIViewExtensions.swift +++ b/Sources/Brave/Extensions/UIViewExtensions.swift @@ -14,6 +14,12 @@ extension UIView { let offset = offset ?? .zero + // Temporary check to handle _UIGraphicsBeginImageContextWithOptions zero size error + // Shiould be replaced with UIGraphicsImageRenderer + if size.width <= 0 || size.height <= 0 { + return nil + } + UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale * quality) drawHierarchy(in: CGRect(origin: offset, size: frame.size), afterScreenUpdates: false) let image = UIGraphicsGetImageFromCurrentImageContext() From c3e84bf9508358e759380ac61920630996837e8b Mon Sep 17 00:00:00 2001 From: Soner Yuksel Date: Tue, 17 Oct 2023 11:53:57 -0400 Subject: [PATCH 2/2] Multiple UIGraphicsBeginImageContextWithOptions handled --- Sources/Brave/Extensions/UIViewExtensions.swift | 4 ++-- Sources/Favicon/BundledFaviconRenderer.swift | 9 ++++----- Sources/Favicon/UIImage+FaviconRenderer.swift | 2 +- .../Callouts/OnboardingPlaylistView.swift | 4 ++-- Sources/Shared/Extensions/UIImageExtensions.swift | 14 +++++++++++++- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Sources/Brave/Extensions/UIViewExtensions.swift b/Sources/Brave/Extensions/UIViewExtensions.swift index c54045a4a36..61dc4699044 100644 --- a/Sources/Brave/Extensions/UIViewExtensions.swift +++ b/Sources/Brave/Extensions/UIViewExtensions.swift @@ -15,8 +15,8 @@ extension UIView { let offset = offset ?? .zero // Temporary check to handle _UIGraphicsBeginImageContextWithOptions zero size error - // Shiould be replaced with UIGraphicsImageRenderer - if size.width <= 0 || size.height <= 0 { + // Should be replaced with UIGraphicsImageRenderer + guard size.width > 0, size.height > 0 else { return nil } diff --git a/Sources/Favicon/BundledFaviconRenderer.swift b/Sources/Favicon/BundledFaviconRenderer.swift index 485bf85c52c..d9afb8c6a5d 100644 --- a/Sources/Favicon/BundledFaviconRenderer.swift +++ b/Sources/Favicon/BundledFaviconRenderer.swift @@ -77,14 +77,13 @@ public class BundledFaviconRenderer { } else if let name = url.baseDomain, let icon = Self.bundledIcons[name] { bundleIcon = icon } - guard let icon = bundleIcon, let image = UIImage(contentsOfFile: icon.url) else { + guard let icon = bundleIcon, + let image = UIImage(contentsOfFile: icon.url), + let scaledImage = image.createScaled(CGSize(width: 40.0, height: 40.0)) else { return nil } - return ( - image.createScaled(CGSize(width: 40.0, height: 40.0)), - icon.color - ) + return (scaledImage, icon.color) } private static let multiRegionDomains = ["craigslist", "google", "amazon"] diff --git a/Sources/Favicon/UIImage+FaviconRenderer.swift b/Sources/Favicon/UIImage+FaviconRenderer.swift index 1ae991ce72e..7a8110599df 100644 --- a/Sources/Favicon/UIImage+FaviconRenderer.swift +++ b/Sources/Favicon/UIImage+FaviconRenderer.swift @@ -49,7 +49,7 @@ extension UIImage { return false } - guard let cgImage = createScaled(CGSize(width: 48.0, height: 48.0)).cgImage else { + guard let cgImage = createScaled(CGSize(width: 48.0, height: 48.0))?.cgImage else { return false } diff --git a/Sources/Onboarding/Callouts/OnboardingPlaylistView.swift b/Sources/Onboarding/Callouts/OnboardingPlaylistView.swift index ac6d1d113db..76617ec6f8b 100644 --- a/Sources/Onboarding/Callouts/OnboardingPlaylistView.swift +++ b/Sources/Onboarding/Callouts/OnboardingPlaylistView.swift @@ -395,9 +395,9 @@ extension OnboardingPlaylistView { .trimmingCharacters(in: .whitespaces) // The icon, which is a PDF since it is multicoloured and has special alpha, needs to scale with // the text, so we need to create a UIImage version of it, scale that, then adjust the baseline offset + let addIcon = UIImage(sharedNamed: "leo.playlist.bold.add")! let icon = Image( - uiImage: UIImage(sharedNamed: "leo.playlist.bold.add")! - .createScaled(CGSize(width: tryItOutIconSize, height: tryItOutIconSize)) + uiImage: addIcon.createScaled(CGSize(width: tryItOutIconSize, height: tryItOutIconSize)) ?? addIcon ) let iconText = Text(icon).baselineOffset(tryItOutIconBaselineOffset) diff --git a/Sources/Shared/Extensions/UIImageExtensions.swift b/Sources/Shared/Extensions/UIImageExtensions.swift index 51e020c1d42..a1cce63e285 100644 --- a/Sources/Shared/Extensions/UIImageExtensions.swift +++ b/Sources/Shared/Extensions/UIImageExtensions.swift @@ -27,7 +27,11 @@ extension UIImage { return image } - public func createScaled(_ size: CGSize) -> UIImage { + public func createScaled(_ size: CGSize) -> UIImage? { + guard size.width > 0, size.height > 0 else { + return nil + } + UIGraphicsBeginImageContextWithOptions(size, false, 0) draw(in: CGRect(size: size)) let scaledImage = UIGraphicsGetImageFromCurrentImageContext() @@ -59,6 +63,10 @@ extension UIImage { } public func textToImage(drawText text: String, textFont: UIFont? = nil, textColor: UIColor? = nil, atPoint point: CGPoint) -> UIImage? { + guard size.width > 0, size.height > 0 else { + return nil + } + let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center @@ -80,6 +88,10 @@ extension UIImage { } public func imageWithInsets(insets: UIEdgeInsets) -> UIImage? { + guard size.width > 0, size.height > 0 else { + return nil + } + UIGraphicsBeginImageContextWithOptions( CGSize(width: self.size.width + insets.left + insets.right, height: self.size.height + insets.top + insets.bottom), false, self.scale)