1

i want to use

func CGContextSetFillPattern(_ c: CGContext!,
                       _ pattern: CGPattern!,
                       _ components: UnsafePointer<CGFloat>)

in my drawing app , to fill with pattern ... how can i implement CGPattern using swift ?? CGContext Reference link

 func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

// 1
UIGraphicsBeginImageContext(view.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))

// 2
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
CGContextAddLineToPoint(context, toPoint.x, toPoint.y)

// 3
CGContextSetLineCap(context, kCGLineCapRound)
CGContextSetLineWidth(context, brushWidth)
CGContextSetRGBStrokeColor(context, red, green, blue, 1.0)
CGContextSetBlendMode(context, kCGBlendModeNormal)

// 4
CGContextStrokePath(context)

// 5
tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
tempImageView.alpha = opacity
UIGraphicsEndImageContext()
  • See https://stackoverflow.com/questions/44211720/trouble-using-callbacks-with-cgpattern-in-swift3 for a Swift 3 version of using CGPattern. – rmaddy May 27 '17 at 02:47

1 Answers1

-1

You can check this links Quartz 2D programming Patterns and CGPattern. In shortcut to use fillpatern you need to write a callback function that draws one instance of pattern, and later you pass the pointer to callback to cgpatterncreate.

Here you have tutorial how you can make it in obj-c cgpatternref obj-c you can easly translate it to swift

Roman Barzyczak
  • 3,785
  • 1
  • 30
  • 44