我想将动画NSView
(或只是底层CALayer
)渲染成一系列图像,而视图根本不显示在屏幕上。我想出了如何做到这一点,CARenderer
但是MTLTexture
下面的方法存在一些问题。
这在操场上运行并将输出存储到Off-screen Render
您下载的文件夹中:
import AppKit
import Metal
import QuartzCore
import PlaygroundSupport
let view = NSView(frame: CGRect(x: 0, y: 0, width: 600, height: 400))
let circle = NSView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
circle.wantsLayer = true
circle.layer?.backgroundColor = NSColor.red.cgColor
circle.layer?.cornerRadius = 25
view.wantsLayer = true
view.addSubview(circle)
let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .rgba8Unorm, width: 600, height: 400, mipmapped: false)
textureDescriptor.usage = [MTLTextureUsage.shaderRead, .shaderWrite, .renderTarget]
let device = MTLCreateSystemDefaultDevice()!
let texture: MTLTexture = device.makeTexture(descriptor: textureDescriptor)!
let context = CIContext(mtlDevice: device)
let renderer = CARenderer(mtlTexture: texture)
renderer.layer = view.layer
renderer.bounds = view.frame
let outputURL: URL = try! FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("Off-screen Render")
try? FileManager.default.removeItem(at: outputURL)
try! FileManager.default.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)
var frameNumber: Int = 0
func render() {
Swift.print("Rendering frame #\(frameNumber)…")
renderer.beginFrame(atTime: CACurrentMediaTime(), timeStamp: nil)
renderer.addUpdate(renderer.bounds)
renderer.render()
renderer.endFrame()
let ciImage: CIImage = CIImage(mtlTexture: texture)!
let cgImage: CGImage = context.createCGImage(ciImage, from: ciImage.extent)!
let url: URL = outputURL.appendingPathComponent("frame-\(frameNumber).png")
let destination: CGImageDestination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypePNG, 1, nil)!
CGImageDestinationAddImage(destination, cgImage, nil)
guard CGImageDestinationFinalize(destination) else { fatalError() }
frameNumber += 1
}
var timer: Timer?
NSAnimationContext.runAnimationGroup({ context in
context.duration = 0.25
view.animator().frame.origin = CGPoint(x: 550, y: 350)
}, completionHandler: {
timer?.invalidate()
render()
Swift.print("Finished off-screen rendering of \(frameNumber) frames in \(outputURL.path)…")
})
// Make the first render immediately after the animation start and after it completes. For the purpose
// of this demo timer is used instead of display link.
render()
timer = Timer.scheduledTimer(withTimeInterval: 1 / 30, repeats: true, block: { _ in render() })
上述代码的问题如下面的附件所示,分别是:
纹理不会被清理,并且每个下一帧都绘制在前一个渲染之上。我知道我可以使用
replace(region:…)
,但怀疑它与具有清晰颜色描述的渲染通道相比效率不高。这是真的?渲染通道可以和 一起使用CARenderer
吗?第一帧(在实际项目中是两到三帧)通常是空的。我怀疑这与
CARenderer
渲染或CGImage
使用 Core Image 构建过程中的一些异步行为有关。如何避免这种情况?纹理上有某种等待直到渲染完成的回调吗?