Mac Catalyst中drawHierarchy截图过亮问题求助(含复现仓库)
Mac Catalyst应用ImageView截图异常问题
我已创建GitHub仓库PrashantKT/ImageViewScreenShotIssue用于复现该问题,这是一个仅含两个页面的应用,专门用来演示问题。
我原本使用以下代码对视图进行截图:
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 } }
这段代码在我将macOS从14.2.1升级至14.4.1、Xcode从15.0升级至15.3前运行完全正常。
问题描述
在Mac Catalyst应用中,使用上述代码对ImageView截图时,得到的画面过亮:
[使用该方法的截图效果]
- 若使用以下方法截图:
func takeSnapshotWithoutScale() -> UIImage? { UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0) if let currentContext = UIGraphicsGetCurrentContext() { self.layer.render(in: currentContext) } let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage }
[方法2的截图效果]
内容的提问来源于stack exchange,提问作者Prashant Tukadiya
相关产品推荐
相关产品推荐

