在 CALayer 中显示 NSImage 时出现问题
init(image: NSImage) {
self.image = image
super.init(frame: .zero)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewWillDraw() {
super.viewWillDraw()
animationLayer.contentsGravity = kCAGravityResizeAspect
animationLayer.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
animationLayer.contents = image.cgImage
animationLayer.masksToBounds = true
animationLayer.backgroundColor = NSColor.blue.cgColor
wantsLayer = true
layer?.addSublayer(animationLayer)
}
backgroundColor
设置为蓝色的 CALayer 渲染良好,但 NSImage
则不然。 NSImage
被确认包含所需的图像,并在类初始化时设置。
在 iOS 框架内开发了相同的内容,我很难看出其中的差异。
最佳答案
与 iOS 不同,macOS 中 CALayer
的 image
属性考虑的是 NSImage
而不是其 CGImage
表示形式。
animationLayer.contents = image
关于macos - Cocoa CALayer 不显示 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41841163/