extension UIView {
func takeSnapshot(rect : CGRect? = CGRect.zero) -> UIImage? {
let renderer = UIGraphicsImageRenderer(size: frame.size)
var image = renderer.image { _ in drawHierarchy(in: bounds, afterScreenUpdates: true) }
if let imageRect = rect, imageRect != CGRect.zero {
let screenshotFrame = CGRect(x: imageRect.origin.x * UIScreen.main.scale, y: imageRect.origin.y * UIScreen.main.scale, width: imageRect.size.width * UIScreen.main.scale, height: imageRect.size.height * UIScreen.main.scale)
let imageRef = image.cgImage!.cropping(to: screenshotFrame)
image = UIImage.init(cgImage: imageRef!, scale: image.scale, orientation: image.imageOrientation)
}
UIGraphicsEndImageContext()
return image
}
}
which was working fine until I updated to macOS 14.4.1 from 14.2.1 and to Xcode 15.3 from 15.0.
issue
From my Mac Catalyst app, if I try to take screenshot of imageView, the screenshot is brighter.
If I try this method it seems working:
func takeSnapshotWithoutScale() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0)
if let currentContext = UIGraphicsGetCurrentContext() {
self.layer.render(in: currentContext)
}
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}