Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #8268: UIGraphicsBeginImageContextWithOptions causing Intermitten…
Browse files Browse the repository at this point in the history
…t crash while taking Tab Screenshot (#8269)
  • Loading branch information
soner-yuksel authored Oct 17, 2023
1 parent bb27505 commit 6a2c21b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Sources/Brave/Extensions/UIViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ extension UIView {

let offset = offset ?? .zero

// Temporary check to handle _UIGraphicsBeginImageContextWithOptions zero size error
// Should be replaced with UIGraphicsImageRenderer
guard size.width > 0, size.height > 0 else {
return nil
}

UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale * quality)
drawHierarchy(in: CGRect(origin: offset, size: frame.size), afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
Expand Down
9 changes: 4 additions & 5 deletions Sources/Favicon/BundledFaviconRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion Sources/Favicon/UIImage+FaviconRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Onboarding/Callouts/OnboardingPlaylistView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 13 additions & 1 deletion Sources/Shared/Extensions/UIImageExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 6a2c21b

Please sign in to comment.