Cocoa CALayer 不显示 NSImage

Ric*_*ppz 1 macos cocoa calayer nsimage swift3

在 CALayer 中显示 NSImage 时出现问题

\n\n
 init(image: NSImage) {\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0self.image = image\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0super.init(frame: .zero)\n }\n\n required init(coder aDecoder: NSCoder) {\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0fatalError("init(coder:) has not been implemented")\n }\n\n override func viewWillDraw() {\n     super.viewWillDraw()\n     animationLayer.contentsGravity = kCAGravityResizeAspect\n     animationLayer.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)\n     animationLayer.contents = image.cgImage\n     animationLayer.masksToBounds = true\n     animationLayer.backgroundColor = NSColor.blue.cgColor\n\n     wantsLayer = true\n     layer?.addSublayer(animationLayer)\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

设置为蓝色的 CALayerbackgroundColor渲染得很好,但 CALayer 则NSImage不然。确认NSImage包含所需的图像并在类初始化时设置。

\n\n

在 iOS 框架内开发了相同的内容后,我很难看出其中的差异。

\n

vad*_*ian 5

与 iOS 不同的是,macOS 中考虑的是image属性而不是其表示。CALayerNSImageCGImage

animationLayer.contents = image
Run Code Online (Sandbox Code Playgroud)