Core Graphics
Creating a Core Graphics Context
Section titled “Creating a Core Graphics Context”Core Graphics context
A Core Graphics context is a canvas which we can draw in it and set some properties like the line thickness.Making a context
Section titled “Making a context”To make a context, we use the UIGraphicsBeginImageContextWithOptions() C function. Then, when we are done with drawing, we just call UIGraphicsEndImageContext() to end the context:
let size = CGSize(width: 256, height: 256)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
let context = UIGraphicsGetCurrentContext()
// drawing code here
UIGraphicsEndImageContext()Objective-C
Section titled “Objective-C”CGSize size = [CGSize width:256 height:256];
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContext *context = UIGraphicsGetCurrentContext();
// drawing code here
UIGraphicsEndImageContext();In the code above, we passed 3 parameters to the UIGraphicsBeginImageContextWithOptions() function:
Presenting the Drawn Canvas to User
Section titled “Presenting the Drawn Canvas to User”let image = UIGraphicsGetImageFromCurrentImageContext()imageView.image = image //assuming imageView is a valid UIImageView objectObjective-C
Section titled “Objective-C”UIImage *image = UIGraphicsGetImageFromCurrentImageContext();imageView.image = image; //assuming imageView is a valid UIImageView object