SALE ENDS TODAY: Save 50% on all Swift books and bundles! >>

Core Graphics

Found 13 articles in the Swift Knowledge Base for this category.

 

How to calculate the distance between two CGPoints

You can calculate the distance between two CGPoints by using Pythagoras's theorem, but be warned: calculating square roots is not fast, so if possible you want to avoid it. More on that in a moment, but first here's the code you need:... Continue Reading >>

How to calculate the Manhattan distance between two CGPoints

Manhattan distance is the distance between two integer points when you are unable to move diagonally. It's named "Manhattan distance" because of the grid-like layout of New York: whether you go four streets up then five streets across, or five streets across then four streets up, or you zig zag to and fro, the actual end distance is identical because you're just moving across a grid.... Continue Reading >>

How to calculate the point where two lines intersect

Finding where two lines cross can be done by calculating their cross product. The code below returns an optional tuple containing the X and Y intersection points, or nil if they don’t cross at all.... Continue Reading >>

How to compare two CGRects with equalTo()

You could compare two CGRect values by evaluating their X, Y, width and height values, but there's a much faster way: equalTo(). This takes two rects as its only two parameters and returns true if they are the same, or false otherwise.... Continue Reading >>

How to draw a circle using Core Graphics: addEllipse(in:)

Core Graphics is able to draw circles and ellipses with just a few lines of code, although there is some set up to do first. The example code below creates a 512x512 circle with a red fill and a black border:... Continue Reading >>

How to draw a square using Core Graphics: addRect()

You can draw a square (or indeed any size of rectangle) using the addRect() Core Graphics function. There's a little bit of set up work required, such as creating a context big enough to hold the square and setting up colors, but the code below does everything you need:... Continue Reading >>

How to draw a text string using Core Graphics

To draw text in Core Graphics is trivial because every Swift string has a built-in draw(with:) method that takes an array of attributes and a position and size. There is, like always, some Core Graphics set up work to do, but this next code snippet is a complete example you can re-use easily:... Continue Reading >>

How to draw lines in Core Graphics: move(to:) and addLine(to:)

You can draw lines in Core Graphics using move(to:) and addLine(to:). The first function moves the Core Graphics path to a CGPoint of your choosing, and the second function moves the path to a new point while also adding a line. Once you add in the required code to set up a context and choose a color, you can draw a triangle with this code:... Continue Reading >>

How to find the rotation from a CGAffineTransform

A CGAffineTransform value combines scale, translation and rotation all at once, but if you just want to know its rotation value is then use this code:... Continue Reading >>

How to find the scale from a CGAffineTransform

If you have a CGAffineTransform and want to know what its scale component is – regardless of whether it has been rotated or translated – use this code:... Continue Reading >>

How to find the translation from a CGAffineTransform

You can pull out the translation from a CGAffineTransform by using the function below. Feed it a transform and it will return you a CGPoint:... Continue Reading >>

How to render a PDF to an image

iOS has built-in APIs for drawing PDFs, which means it's relatively straight forward to render a PDF to an image. I say "relatively" because there's still some boilerplate you need to worry about: figuring out the document size, filling the background in a solid color to avoid transparency, and flipping the rendering so that the PDF draws the right way up.... Continue Reading >>

How to use Core Graphics blend modes to draw a UIImage differently

If you're rendering images using Core Graphics you should definitely try out some of the alternate blend modes that are available. If you've ever used Photoshop's blend modes these will be familiar: screen, luminosity, multiply and so on – these are all available right in Core Graphics.... Continue Reading >>

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions, all written for Swift 5.1.

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!