【问题标题】:Cocoa CALayer not displaying NSImage可可 CALayer 不显示 NSImage
【发布时间】:2017-01-25 00:03:28
【问题描述】:

在 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 框架中开发了相同的东西,我很难看出区别。

【问题讨论】:

    标签: macos cocoa swift3 calayer nsimage


    【解决方案1】:

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

    animationLayer.contents = image
    

    【讨论】:

    • 删除了我的最后一条评论,因为它确实有效;)谢谢
    猜你喜欢
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多