macos - Cocoa CALayer 不显示 NSImage

标签 macos cocoa swift3 calayer 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 被确认包含所需的图像,并在类初始化时设置。

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

最佳答案

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

animationLayer.contents = image

关于macos - Cocoa CALayer 不显示 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41841163/

相关文章:

c - OS X 上的弱符号别名类似于 Linux 上的弱符号别名,还是最接近的等价物?

c - 体系结构 x86_64 的 undefined symbol "_min"

cocoa - NSPopUpButton 箭头颜色

ios - 重命名 View Controller 后 xcode iOS 应用程序崩溃

ios - 如何将帖子字符串写入API swift 3

macos - 测试属性是否存在(即 numberOfValidItemsForDrop)

objective-c - AVAssetImageGenerator复制CGImageAtTime :actualTime:error: generates 8-bit images from 10-bit video

cocoa - addSubview 和自动调整大小

ios - 当应用程序处于后台或被杀死时如何定期获取新位置

objective-c - NSWindow 注册鼠标事件(使用 NSTrackingArea),即使应用程序未获得焦点?