【问题标题】:How to convert COpaquePointer in swift to some type (CGContext? in particular)如何将 COpaquePointer 快速转换为某种类型(特别是 CGContext?)
【发布时间】:2014-06-03 07:21:32
【问题描述】:

我在 swift 中有以下无法编译的代码:

class CustomView: NSView {
  override func drawRect(dirtyRect: NSRect) {
    var contextPointer: COpaquePointer = NSGraphicsContext.currentContext()!.graphicsPort()
    var context: CGContext? = contextPointer as? CGContext
    CGContextSetRGBFillColor(context, 1, 0, 0, 1)
    CGContextFillRect(context, CGRectMake(0, 0, 100, 100))
    CGContextFlush(context)
  }
}

如何将 COpaquePointer 转换为 CGContext?

【问题讨论】:

    标签: swift


    【解决方案1】:

    从 Developer Preview 5 开始,NSGraphicsContext 现在有一个 CGContext 属性。它还没有记录,但它在头文件中:

    @property (readonly) CGContextRef CGContext NS_RETURNS_INNER_POINTER NS_AVAILABLE_MAC(10_10);
    

    【讨论】:

      【解决方案2】:

      这有点丑;如果 graphicsPort() 方法被更新为直接返回 CGContextRef(如UIGraphicsGetCurrentContext()),而不是 void* 会更好。我将此扩展添加到 NSGraphicsContext 以暂时隐藏它:

      extension NSGraphicsContext {
        var cgcontext: CGContext {
          return Unmanaged<CGContext>.fromOpaque(self.graphicsPort()).takeUnretainedValue()
        }
      }
      

      只需在需要 CGContext 的任何地方致电 NSGraphicsContext.currentContext().cgcontext

      【讨论】:

      • 我不知道UnmanagedfromOpaque,但这很好用!谢谢!
      • 但是,使用reinterpretCast 大锤似乎可以工作
      • 什么reinterpretCast?我将它输入到 Xcode 文档搜索中,但一无所获。新的行代码会有所帮助。
      • 这工作:return reinterpretCast(self.graphicsPort());这是example gist
      【解决方案3】:

      这似乎正在编译:

      var contextPointer = NSGraphicsContext.currentContext()!.graphicsPort()
      let context = UnsafePointer<CGContext>(contextPointer).memory
      

      【讨论】:

      • 谢谢!不确定您是否需要使用 .currentContext()!虽然 - currentContext 的类型不是已经解包了吗? class func currentContext() -&gt; NSGraphicsContext!
      • @alaroldai 是的,但我需要 CGContext 而不是 NSGraphicsContext
      【解决方案4】:

      你快到了。试试这段代码

      class CustomView: NSView {
          override func drawRect(dirtyRect: NSRect) {
      
              var contextPointer = NSGraphicsContext.currentContext()!.graphicsPort()
              var context = UnsafePointer<CGContext>(contextPointer).memory
      
              CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0)
              CGContextFillRect(context, CGRectMake(0, 0, 100, 100))
              CGContextFlush(context)
          }
      }
      

      【讨论】:

        【解决方案5】:

        在 Xcode 6.3 / Swift 1.2 中,NSGraphicsContext.currentContext()!.graphicsPort 返回 UnsafeMutablePointer&lt;Void&gt;,您可以使用 unsafeBitCast 将其转换为 CGContextRef,因为它只是一个 C 指针。这只是重新解释指针值,因此应该与 C 强制点转换完全相同。

            let p1  =   NSGraphicsContext.currentContext()!.graphicsPort
            let ctx =   unsafeBitCast(p1, CGContextRef.self)
        

        请注意,这种重新解释演员总是非常危险的,只有在您确定自己在做什么时才使用。

        【讨论】:

          猜你喜欢
          • 2020-11-09
          • 2016-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-30
          • 1970-01-01
          • 2012-05-20
          相关资源
          最近更新 更多