Swift: Giving Context To Cgcontext (Part I)

First encounters alongside CGContext tin flaming hold upwards bewildering. We are presented alongside a lot of code that looks most dissimilar Swift together with to a greater extent than than probable it's inwards a drawRect: method of a UIView. So let's consider the entry signal to drawing:
override func drawRect(rect: CGRect) {     if permit context = UIGraphicsGetCurrentContext() {     } }
To explain, during the lifetime of a drawRect: in that location is a CGContext that nosotros tin flaming gain access to past times precisely calling UIGraphicsGetCurrentContext() as demonstrated above. Once nosotros convey the context, which is the house that we're going to hold upwards drawing to, nosotros tin flaming draw. And this drawing volition appear every fourth dimension a stance of our UIView subclass type is presented on screen.

Demonstrating CGContext

To demonstrate this, the showtime affair we're going to practise is to fill upwards the stance together with to clitoris a line of piece of employment closed to it. But the showtime affair whatever author of Swift code volition abide by is that placing a point later the context instance get upwards results inwards no options popping upwards whatsoever. So what tin flaming nosotros truly practise alongside this context? Well nosotros convey to utilization a attain of functions similar so:
// drawing code CGContextAddRect(context, rect) CGContextSetFillColorWithColor(context, fillColor.CGColor) CGContextSetStrokeColorWithColor(context, strokeColor.CGColor) CGContextSetLineWidth(context, width) CGContextDrawPath(context, CGPathDrawingMode.FillStroke) 
And each fourth dimension we're specifying the context as an declaration inwards add-on to other details to larn things done. For example, hither nosotros desire to add together a rectangle to the context together with non alone does it presently decease fairly tardily writing long role names together with repeating 'context' but if we're unremarkably creating rectangles alongside a fill upwards colour together with an outline colour hence why non precisely convey an extension that clears away the ask for writing all this over together with over ane time again together with that also makes everything far to a greater extent than Swift-like:
extension CGContextRef {     func addRect(rect:CGRect, fillColor:UIColor, strokeColor:UIColor, width:CGFloat) {         CGContextAddRect(self, rect)         self.fillAndStroke(fillColor, strokeColor: strokeColor, width: width)      }     func fillAndStroke(fillColor:UIColor, strokeColor:UIColor, width:CGFloat) {         CGContextSetFillColorWithColor(self, fillColor.CGColor)         CGContextSetStrokeColorWithColor(self, strokeColor.CGColor)         CGContextSetLineWidth(self, width)         CGContextDrawPath(self, CGPathDrawingMode.FillStroke)     }  } 
So directly nosotros tin flaming write simply:
context.addRect(CGRect(x: 100, y: 100, width: 200, height: 200), fillColor: UIColor.blueColor(), strokeColor: UIColor.greenColor(), width: 5) 
And nosotros tin flaming add together a filled together with stroked rectangle to our stance without all the CGContext... functions getting inwards the way.

The total UIView subclass drawRect: method looking similar this:
override func drawRect(rect: CGRect) {     if permit context = UIGraphicsGetCurrentContext() {         context.addRect(CGRect(x: 100, y: 100, width: 200, height: 200), fillColor: UIColor.blueColor(), strokeColor: UIColor.greenColor(), width: 5)     } }
Remembering that the higher upwards CGContextRef extensions also ask including inwards our project.

UIBezierPath

Influenza A virus subtype H5N1 similar functionality already exists via UIBezierPath, which Apple describes as "an Objective-C wrapper for the path-related features inwards the Core Graphics framework". And y'all tin flaming also create UIBezierPaths within the drawRect: leveraging ane time once again the context but without e'er calling UIGraphicsGetCurrentContext().
let bez = UIBezierPath(rect: CGRect(x: 100, y: 100, width: 200, height: 200))             UIColor.greenColor().setFill()             UIColor.whiteColor().setStroke()             bez.lineWidth = fifteen             bez.fill()             bez.stroke()                          permit bez2 = UIBezierPath(rect: CGRect(x: 200, y: 200, width: 100, height: 100))             UIColor.greenColor().setFill()             UIColor.whiteColor().setStroke()             bez2.lineWidth = fifteen             bez2.fill()             bez2.stroke() 
Notice how UIColor provides the setFill together with setStroke methods. We don't ask whatever conversion hither betwixt UI together with CG equivalents. This makes things ameliorate but it tin flaming experience every bit mysterious almost how the path is drawn without whatever acknowledgement of the context together with every bit no addSubview or addSublayer type methods happening either.

UIBezierPath exterior the drawRect

Outside the drawRect of a UIView y'all tin flaming utilization UIBezierPath inwards combination alongside CAShapeLayer, together with this is where it feels to a greater extent than natural to me. Although this approach consumes an extra brace of lines of code when placed within a UIViewController subclass, as here, than it would within a UIView drawRect: method:
        permit bez = UIBezierPath(rect: CGRect(x: 100, y: 100, width: 200, height: 200))         permit shape = CAShapeLayer()         shape.path = bez.CGPath         shape.frame = view.frame         shape.fillColor = UIColor.greenColor().CGColor         shape.strokeColor = UIColor.blueColor().CGColor         shape.lineWidth = fifteen         view.layer.addSublayer(shape) 
And complicates things for elementary drawing such as we're doing here.

Conclusion

I am aware that this postal service is fossil oil closed to the edges (and inwards the middle too!) but I wanted to brand a start on approaching CGContext together with its wrapper, UIBezierPath. It is something that I convey plans to render to inwards the shape of bitmap drawing together with perhaps PDF but we'll see how that goes.

Comments

Popular posts from this blog

Removing The Index.Php File From Url Inward Codeigniter

What Are The Main Components of a Computer System

Delete Daily Doppler E-Mail Spam From An Iphone [Fix]