Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call CGPatternCreate in Swift

I'm wondering how to convert the following objective-c method to Swift function?

CGPatternRef pattern = CGPatternCreate(NULL,
                                       rect,
                                       CGAffineTransformIdentity,
                                       24,
                                       24,
                                       kCGPatternTilingConstantSpacing,
                                       true,
                                       &callbacks);

My code:

    let callbacks : CGPatternCallbacks = CGPatternCallbacks(version: 0)

    let pattern : CGPatternRef = CGPatternCreate(nil,
        rect,
        CGAffineTransformIdentity,
        24,
        24,
        kCGPatternTilingConstantSpacing,
        true,
        callbacks)

But I got an error message:

'CGPatternCallbacks' is not convertible to 'CConstPointer'

Is there any sample code for this? Thanks

like image 329
Bagusflyer Avatar asked Nov 20 '25 03:11

Bagusflyer


2 Answers

Something like that

var callbacks : CGPatternCallbacks = CGPatternCallbacks(version: 0)

var pattern = CGPatternCreate(nil,
    rect,
    CGAffineTransformIdentity,
    24,
    24,
    kCGPatternTilingConstantSpacing,
    true,
    &callbacks)
like image 195
B.S. Avatar answered Nov 22 '25 17:11

B.S.


This solution is a problematic one:

The pointer registered within CGPatternCallbacks (for a function that draws the pattern) should be CFunctionPointer<(UnsafeMutablePointer, CGContext>)->Void) This means that the function pointer should be transformed to UnasfeMutablePointer then to COpaquePointer then to CFuncfionPointer

And still anyway i'm getting an exception on function call, there is a simplier solution: [http://www.raywenderlich.com/90695/modern-core-graphics-with-swift-part-3][1]

like image 21
Yan Avatar answered Nov 22 '25 16:11

Yan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!