Cocoa CALayer 不显示 NSImage

Cocoa CALayer not displaying NSImage

在 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 被确认为包含所需的图像,并在 class.

的初始化时设置

在 iOS 框架内开发了相同的东西,我很难看出区别。

与 iOS 不同,macOS 中 CALayerimage 属性 考虑 NSImage 而不是其 CGImage 表示。

animationLayer.contents = image