DrawRect and drawRect
Before starting, we need to create a DrawRectView
The initial code is
//// DrawRectView. h // CGContextSetShouldAntialias // Created by YouXianMing on. // Copyright 2017 TechCode. all rights reserved. // # import <UIKit/UIKit. h> @ interface DrawRectView: UIView @ end
//// DrawRectView. m // CGContextSetShouldAntialias // Created by YouXianMing on. // Copyright 2017 TechCode. all rights reserved. // # import "DrawRectView. h "@ implementation DrawRectView-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {self. backgroundColor = [UIColor clearColor]; self. layer. borderWidth = 0.5f; self. layer. borderColor = [UIColor redColor]. CGColor;} return self;} @ end
Used in ViewController (X in size and centered)
The display effect is as follows (the border is displayed with a red border)
The following code modifies DrawRectView. m:
//// DrawRectView. m // CGContextSetShouldAntialias // Created by YouXianMing on. // Copyright 2017 TechCode. all rights reserved. // # import "DrawRectView. h "@ implementation DrawRectView-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {self. backgroundColor = [UIColor clearColor]; self. layer. borderWidth = 0.5f; self. layer. borderColor = [UIColor redColor]. CGColor;} return self;}-(void) drawRect :( CGRect) rect {// Set stroke color [[UIColor blackColor] setStroke]; // Draw 7 lines. for (int I = 0; I <7; I ++) {UIBezierPath * path = [UIBezierPath bezierPath]; path. lineWidth = 0.5f; [path moveToPoint: CGPointMake (10, 10 + I * 10.3)]; [path addLineToPoint: CGPointMake (10 + 80, 10 + I * 10.3)]; [path stroke] ;}}@ end
In fact, the following drawing code is added to draw 7 lines, with the width of each line being 0.5
The effect is as follows:
After enlarging the image, you will find that the width of the line is inconsistent. Some colors are deep and some are light. This is the effect after anti-aliasing is enabled.
Modify code to disable anti-aliasing
//// DrawRectView. m // CGContextSetShouldAntialias // Created by YouXianMing on. // Copyright 2017 TechCode. all rights reserved. // # import "DrawRectView. h "@ implementation DrawRectView-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {self. backgroundColor = [UIColor clearColor]; self. layer. borderWidth = 0.5f; self. layer. borderColor = [UIColor redColor]. CGColor;} return self;}-(void) drawRect :( CGRect) rect {// Get context. CGContextRef context = UIGraphicsGetCurrentContext (); // Sets anti-aliasing on. CGContextSetShouldAntialias (context, NO); // Set stroke color [[UIColor blackColor] setStroke]; // Draw 7 lines. for (int I = 0; I <7; I ++) {UIBezierPath * path = [UIBezierPath bezierPath]; path. lineWidth = 0.5f; [path moveToPoint: CGPointMake (10, 10 + I * 10.3)]; [path addLineToPoint: CGPointMake (10 + 80, 10 + I * 10.3)]; [path stroke] ;}}@ end
Display Effect
After the image is enlarged, the line width is consistent.
Conclusion
After anti-aliasing is enabled, the system will blur the drawn lines to make it difficult to see the fangs. What is the fangs? You can run the following code to see the difference between the two.
//// DrawRectView. m // CGContextSetShouldAntialias // Created by YouXianMing on. // Copyright 2017 TechCode. all rights reserved. // # import "DrawRectView. h "@ implementation DrawRectView-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {self. backgroundColor = [UIColor clearColor]; self. layer. borderWidth = 0.5f; self. layer. borderColor = [UIColor redColor]. CGColor;} return self;}-(void) drawRect :( CGRect) rect {// Get context. CGContextRef context = UIGraphicsGetCurrentContext (); // Sets anti-aliasing off. CGContextSetShouldAntialias (context, NO); // Set stroke color [[UIColor blackColor] setStroke]; // Draw 7 lines. for (int I = 0; I <7; I ++) {UIBezierPath * path = [UIBezierPath bezierPath]; path. lineWidth = 0.5f; [path moveToPoint: CGPointMake (0, 0 + I * 10.3)]; [path addLineToPoint: CGPointMake (10 + 80, 10 + I * 10.3)]; [path stroke] ;}}@ end
After the anti-aliasing function is disabled, the image will not be blurred, and the image will also appear.
The line is blurred after the anti-aliasing function is enabled, which is called anti-aliasing.