在Swift3中将回调与CGPattern一起使用时出现问题

rma*_*ddy 4 core-graphics callback swift

我正在尝试CGPattern在Swift中使用彩色图案。Apple在Quartz 2D编程指南中的“ 绘制彩色图案”部分提供了一个不错的Objective-C示例。但是,从Objective-C转换所有语法并不是一件容易的事。另外,我想info在绘图回调中使用该参数,并且没有这样做的示例。

这是我的第一次尝试:

class SomeShape {
    func createPattern() -> CGPattern? {
        let bounds = CGRect(x: 0, y: 0, width: someWidth, height: someHeight)
        let matrix = CGAffineTransform.identity
        var callbacks = CGPatternCallbacks(version: 0, drawPattern: nil, releaseInfo: nil)

        let res = CGPattern(info: nil, bounds: bounds, matrix: matrix, xStep: bounds.width, yStep: bounds.height, tiling: .noDistortion, isColored: true, callbacks: &callbacks)

        return res
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,这需要一个合适的drawPattern参数值CGPatternCallbacks,我需要将其self作为info参数传递给CGPattern初始化程序。

完成此操作的正确语法是什么?

rma*_*ddy 5

让我们先看看CGPatternDrawPatternCallback。它被定义为:

typealias CGPatternDrawPatternCallback = (UnsafeMutableRawPointer?, CGContext) -> Void
Run Code Online (Sandbox Code Playgroud)

所以它是一个带有两个参数的闭包 -info和绘图上下文。

有了这些信息,您可以创建CGPatternCallback如下:

var callbacks = CGPatternCallbacks(version: 0, drawPattern: { (info, ctx) in
    // Drawing code here
}, releaseInfo: { (info) in {
    // Cleanup code here
})
Run Code Online (Sandbox Code Playgroud)

但这里有一些重要的事情需要注意。这些闭包的主体无法捕获块之外的任何内容。如果您尝试这样做,您将收到以下错误:

AC 函数点不能由捕获上下文的闭包形成

这就是为什么info需要使用该参数的原因。您可以在创建模式时将self或其他对象作为info参数传递并在绘图回调中使用它。但这不是一项简单的任务,因为您不能简单地将其self作为info参数传递。您需要将其设置为所需的UnsafeMutableRawPointer,然后将其从绘图回调内的指针转换回来。

这是所有设置的完整代码:

class SomeShape {
    func createPattern() -> CGPattern? {
        let bounds = CGRect(x: 0, y: 0, width: someWidth, height: someHeight) // The size of each tile in the pattern
        let matrix = CGAffineTransform.identity // adjust as needed
        var callbacks = CGPatternCallbacks(version: 0, drawPattern: { (info, ctx) in
            let shape = unsafeBitCast(info, to: SomeShape.self)

            // The needed drawing code to draw one tile of the pattern into "ctx"
        }, releaseInfo: { (info) in 
            // Any cleanup if needed
        })

        let unsafeSelf = unsafeBitCast(self, to: UnsafeMutableRawPointer.self)

        let res = CGPattern(info: unsafeSelf, bounds: bounds, matrix: matrix, xStep: bounds.width, yStep: bounds.height, tiling: .noDistortion, isColored: true, callbacks: &callbacks)

        return res
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用CGPattern,您可以执行以下操作:

func draw(_ ctx: CGContext) {
    // Any other needed setup

    let path = CGPath(....) // some path

    // Code to fill a path with the pattern
    ctx.saveGState()
    ctx.addPath(path) // The path to fill

    // Setup the pattern color space for the colored pattern
    if let cs = CGColorSpace(patternBaseSpace: nil) {
        ctx.setFillColorSpace(cs)
    }

    // Create and apply the pattern and its opacity
    if let fillPattern = someShapeInstance.createPattern() {
        var fillOpacity = CGFloat(1.0)
        ctx.setFillPattern(fillPattern, colorComponents: &strokeOpacity)
    }

    ctx.fillPath(using: theDesiredFillRule)
    ctx.restoreGState()

    // Any other drawing
}
Run Code Online (Sandbox Code Playgroud)

使用彩色图案(相对于模板、非彩色图案)时,您必须在设置填充图案之前设置填充颜色空间。

您还可以使用图案来描边路径。只需使用setStrokeColorSpacesetStrokePattern


Ham*_*ish 5

正如您在回答中所说,CGPatternDrawPatternCallback定义为:

typealias CGPatternDrawPatternCallback =
                               @convention(c) (UnsafeMutableRawPointer?, CGContext) -> Void
Run Code Online (Sandbox Code Playgroud)

@convention(c)属性(似乎只显示在生成的标头中)意味着所使用的函数值必须与C兼容,因此不能捕获任何上下文(因为C函数值只不过是指向函数的原始指针,所以不要t存储其他上下文对象)。

因此,如果要在函数中提供上下文,则需要将自己的上下文传递UnsafeMutableRawPointer?Initializerinfo:参数。然后在调用时将其作为给定绘制模式函数的第一个参数传递。CGPattern

为了传递self给该参数,您可以使用Unmanaged。这使您可以在引用和不透明指针之间进行转换,并且不同于unsafeBitCast,还可以在进行引用时控制引用的内存管理。

鉴于我们不能保证调用方createPattern()self保留,我们不能仅将其传递给info:参数而不自己保留。如果传递时没有保留(例如,使用unsafeBitCast),然后在绘制图案之前将其释放- 在图形回调中尝试使用悬空指针时,您将得到未定义的行为

Unmanaged

  • 您可以将引用作为+1保留的不透明指针传递给 passRetained(_:).toOpaque()

  • 您可以使用以下命令从此指针取回引用fromOpaque(_:).takeUnretainedValue()(实例将保持不变)

  • 然后,您可以通过使用+1保留fromOpaque(_:).release()CGPattern释放时,您需要执行此操作。

例如:

class SomeShape {
    // the bounds of the shape to draw
    let bounds = CGRect(x: 0, y: 0, width: 40, height: 40)

    func createPattern() -> CGPattern? {

        var callbacks = CGPatternCallbacks(version: 0, drawPattern: { info, ctx in

            // cast the opaque pointer back to a SomeShape reference.
            let shape = Unmanaged<SomeShape>.fromOpaque(info!).takeUnretainedValue()

            // The code to draw a single tile of the pattern into "ctx"...
            // (in this case, two vertical strips)
            ctx.saveGState()
            ctx.setFillColor(UIColor.red.cgColor)
            ctx.fill(CGRect(x: 0, y: 0,
                            width: shape.bounds.width / 2, height: shape.bounds.height))

            ctx.setFillColor(UIColor.blue.cgColor)
            ctx.fill(CGRect(x: 20, y: 0,
                            width: shape.bounds.width / 2, height: shape.bounds.height))
            ctx.restoreGState()

        }, releaseInfo: { info in
            // when the CGPattern is freed, release the info reference,
            // consuming the +1 retain when we originally passed it to the CGPattern.
            Unmanaged<SomeShape>.fromOpaque(info!).release()
        })

        // retain self before passing it off to the info: parameter as an opaque pointer.
        let unsafeSelf = Unmanaged.passRetained(self).toOpaque()

        return CGPattern(info: unsafeSelf, bounds: bounds, matrix: .identity,
                         xStep: bounds.width, yStep: bounds.height,
                         tiling: .noDistortion, isColored: true, callbacks: &callbacks)
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,如果您想要一个更好的解决方案,则SomeShape可以将其设置为struct。然后,在创建模式时,您可以将其包装在一个Context堆分配的框中,然后再将其传递给info:参数:

struct SomeShape {

    // the bounds of the shape to draw
    let bounds = CGRect(x: 0, y: 0, width: 40, height: 40)

    func createPattern() -> CGPattern? {

        final class Context {
            let shape: SomeShape
            init(_ shape: SomeShape) { self.shape = shape }
        }

        var callbacks = CGPatternCallbacks(version: 0, drawPattern: { info, ctx in

            // cast the opaque pointer back to a Context reference,
            // and get the wrapped shape instance.
            let shape = Unmanaged<Context>.fromOpaque(info!).takeUnretainedValue().shape

            // ...

        }, releaseInfo: { info in
            // when the CGPattern is freed, release the info reference,
            // consuming the +1 retain when we originally passed it to the CGPattern.
            Unmanaged<Context>.fromOpaque(info!).release()
        })

        // wrap self in our Context box before passing it off to the info: parameter as a
        // +1 retained opaque pointer.
        let unsafeSelf = Unmanaged.passRetained(Context(self)).toOpaque()

        return CGPattern(info: unsafeSelf, bounds: bounds, matrix: .identity,
                         xStep: bounds.width, yStep: bounds.height,
                         tiling: .noDistortion, isColored: true, callbacks: &callbacks)
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,这也可以解决任何保留周期问题。