我可以使用下面的代码将任何SwiftUI视图转换为高分辨率UIImage。它效果很好......直到我尝试使用大于CGSize(宽度:2730,高度:2730)的图像大小。
如果我将图像大小增加到CGSize(宽度:2731,高度:2731)或更大,则会出现以下问题:
在 "extension UIView" 中,无法再绘制UIImage。
有没有任何想法是什么原因导致了尺寸的限制?
一个注意事项:我可以通过取消注释在 "extension View" 中的2行代码并替换来克服尺寸限制:
在“扩展UIView”中......但是,使用“layer.render”将无法呈现像“blur”、SceneKit子视图或金属等图像效果。因此,必须使用“self.drawHierarchy”。
如果我将图像大小增加到CGSize(宽度:2731,高度:2731)或更大,则会出现以下问题:
self.drawHierarchy(in: self.layer.bounds, afterScreenUpdates: true)
在 "extension UIView" 中,无法再绘制UIImage。
有没有任何想法是什么原因导致了尺寸的限制?
一个注意事项:我可以通过取消注释在 "extension View" 中的2行代码并替换来克服尺寸限制:
self.drawHierarchy(in: self.layer.bounds, afterScreenUpdates: true)
使用:
layer.render(in: context.cgContext)
在“扩展UIView”中......但是,使用“layer.render”将无法呈现像“blur”、SceneKit子视图或金属等图像效果。因此,必须使用“self.drawHierarchy”。
// 1: Set breakpoint on line: print("done") to inspect the high res image
// 2: Run, then tap the image on screen to inspect the highresImage
// 3: repeat after changing the size to CGSize = CGSize(width: 2731, height: 2731)
import SwiftUI
struct ContentView: View {
@State var blurRadius: CGFloat = 4.0
let imageSize: CGSize = CGSize(width: 2730, height: 2730)
var body: some View {
testView
.frame(width: 300, height: 300)
.onTapGesture {
// Adjust blur radius based on high res image scale
blurRadius *= imageSize.width * 0.5/300
// Capture high res image of swiftUI view
let highresImage = testView.asImage(size: imageSize)
// set breakpoint here to inspect the high res image size, quality, etc.
print("done")
// reset blur radius back to 4
blurRadius = 4
}
}
var testView: some View {
ZStack {
Color.blue
Circle()
.fill(Color.red)
}
.blur(radius: blurRadius)
}
}
extension UIView {
func asImage() -> UIImage {
let format = UIGraphicsImageRendererFormat()
format.scale = 1
return UIGraphicsImageRenderer(size: self.layer.frame.size, format: format).image { context in
self.drawHierarchy(in: self.layer.bounds, afterScreenUpdates: true)
//layer.render(in: context.cgContext)
}
}
}
extension View {
func asImage(size: CGSize) -> UIImage {
let controller = UIHostingController(rootView: self)
controller.view.bounds = CGRect(origin: .zero, size: size)
//UIApplication.shared.windows.first!.rootViewController?.view.addSubview(controller.view)
let image = controller.view.asImage()
//controller.view.removeFromSuperview()
return image
}
}
8192.0 / UIScreen.main.nativeScale
,您仍然可以在3x iPhone上呈现8192x8192的图像。Apple可能硬编码了8192x8192像素的限制以渲染视图。 - Christopher Boyd