There are several basic plotting methods and several plotting Methods

Source: Internet
Author: User

There are several basic plotting methods and several plotting Methods

1. drawRect:

Rewrite the UIView subclass

2. drawLayer: inContext:

CALayer sets proxy (this is a proxy method)

3. drawInContext:

CALayer subclass Rewriting

4. Use the image context to generate an image:

ImageContext

Try to avoid mixing

------- Implementation of drawRect: Method ----------

1. Use UIKit

      
/** 1. Implement drawRect in the UIView subclass: Method 2: Draw the drawRect in the current context provided by UIKit */-(void) drawRect :( CGRect) rect {// besser path depicts the elliptical UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect: CGRectMake (100,100,200,100)]; // color fill [[UIColor blueColor] setFill]; // fill in the current context [path fill];}
View Code

:

      

2. Use Core Graphic

     
/** 1. Implement drawRect in the UIView subclass: method 2. Use Core Graphics to obtain the current context for plotting */-(void) drawRect :( CGRect) rect {// obtain the current context CGContextRef context = UIGraphicsGetCurrentContext (); // draw a graphic watermark (context, CGRectMake (100,100,200,100); // set the fill color CGContextSetFillColorWithColor (context, context, [UIColor redColor]. CGColor); // render CGContextFillPath (context );}
View Code

:

      

Bytes -----------------------------------------------------------------------------------------------

------- Proxy implementation drawLayer: inContext: Method ----------

1. Use UIKit

      
@ Implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // 1. create a custom layer CALayer * layer = [CALayer layer]; // 2. sets the layer attribute layer. backgroundColor = [UIColor brownColor]. CGColor; layer. bounds = CGRectMake (0, 0,200,150); layer. anchorPoint = CGPointZero; layer. position = CGPointMake (100,100); layer. cornerRadius = 20; layer. shadowColor = [UIColor redColor]. CGColor; layer. shadowOffset = CGSizeMake (10, 20); layer. shadowOpacity = 0.6; // sets the proxy layer. delegate = self; // trigger the proxy method to draw [layer setNeedsDisplay]; // 3. add layer [self. view. layer addSublayer: layer];}-(void) drawLayer :( CALayer *) layer inContext :( CGContextRef) ctx {// converts the referenced context to the current context. UIGraphicsPushContext (ctx); // draw the image UIBezierPath * p = [UIBezierPath layout: CGRectMake (,)]; [[UIColor blueColor] setFill]; [p fill]; UIGraphicsPopContext () ;}@ end
View Code

:

      

2. Use Core Graphic

      
@ Implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // 1. create a custom layer CALayer * layer = [CALayer layer]; // 2. sets the layer attribute layer. backgroundColor = [UIColor brownColor]. CGColor; layer. bounds = CGRectMake (0, 0,200,150); layer. anchorPoint = CGPointZero; layer. position = CGPointMake (100,100); layer. cornerRadius = 20; layer. shadowColor = [UIColor redColor]. CGColor; layer. shadowOffset = CGSizeMake (10, 20); layer. shadowOpacity = 0.6; // sets the proxy layer. delegate = self; [layer setNeedsDisplay]; // 3. add layer [self. view. layer addSublayer: layer];}-(void) drawLayer :( CALayer *) layer inContext :( CGContextRef) ctx {ctx (ctx, CGRectMake (50,50, 100,50); CGContextSetFillColorWithColor (ctx, [UIColor blueColor]. CGColor); CGContextFillPath (ctx) ;}@ end
View Code

:

      

Bytes -----------------------------------------------------------------------------------------------

------- Customize CALayer to override drawInContext :----------

1. Use UIKit

Custom Layer class:

        
# Import "DXLayer. h "# import <UIKit/UIKit. h> @ implementation DXLayer // rewrite this method and draw the image-(void) drawInContext :( CGContextRef) ctx {UIGraphicsPushContext (ctx); // 1. drawing a graphic UIBezierPath * p = [UIBezierPath labels: CGRectMake (50, 50,)]; [[UIColor blueColor] setFill]; [p fill]; UIGraphicsPopContext ();} @ end
View Code

Usage:

        
@ Implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // 1. create a custom layer DXLayer * layer = [DXLayer layer]; // 2. sets the layer attribute layer. backgroundColor = [UIColor brownColor]. CGColor; layer. bounds = CGRectMake (0, 0,200,150); layer. anchorPoint = CGPointZero; layer. position = CGPointMake (100,100); layer. cornerRadius = 20; layer. shadowColor = [UIColor yellowColor]. CGColor; layer. shadowOffset = CGSizeMake (5, 5); layer. shadowOpacity = 0.6; // 3. drawInContext for triggering a layer: Draw a Layer on a custom layer [layer setNeedsDisplay]; // 4. add layer [self. view. layer addSublayer: layer];} @ end
View Code

:

      

2. Use Core Graphic

      
# Import "DXLayer. h "@ implementation DXLayer // rewrite this method and draw the image-(void) drawInContext :( CGContextRef) ctx {// 1. draw a graph // draw a circle CGContextAddEllipseInRect (ctx, CGRectMake (50, 50,100,100); // set the attribute (color) // [[UIColor yellowColor] set]; CGContextSetRGBFillColor (ctx, 0, 0, 1, 1); // 2. rendering CGContextFillPath (ctx);} @ end
View Code

:

      

Bytes -----------------------------------------------------------------------------------------------

------- Use Image Context ----------

1. Use ImageContext to generate a UIImage object

2. There is no limit on the code location. Both methods are restricted to drawRect: Or drawLayer: inContext: method.

 

1. Use UIKit

      
@ Implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; UIImageView * imgView = [[UIImageView alloc] initWithFrame: CGRectMake (100,100,200,100)]; imgView. image = [self createImageFromImageContext]; [self. view addSubview: imgView];}/** create an image object (draw a circle) */-(UIImage *) createImageFromImageContext {/** Create Image context 1: size of the image to be created 2: specify whether the background of the generated image is not transparent 3: Specify the scaling factor of the generated image, which corresponds to the scale attribute of the UIImage The meaning is consistent. Input 0 indicates that the scaling factor of the image is changed according to the screen resolution */uigraphicsbeginimagecontextwitexceptions (CGSizeMake (200,100), NO, 0 ); // drawing UIBezierPath * p = [UIBezierPath bezierPathWithOvalInRect: CGRectMake (200,100,)]; [[UIColor blueColor] setFill]; [p fill]; // obtain the image object UIImage * img = UIGraphicsGetImageFromCurrentImageContext () from the context; // disable the image context UIGraphicsEndImageContext (); return img;} @ end
View Code

:

      

2. Use Core Graphic

      
/** Create an image object by drawing (drawing a circle) */-(UIImage *) createImageFromImageContext {/** Create Image context 1: size of the image to be created 2: specify whether the background of the generated image is not transparent. 3: Specify the scaling factor of the generated image, which is consistent with the meaning indicated by the scale attribute of the UIImage. If 0 is input, the scaling factor of the image is changed according to the screen resolution */resize (CGSizeMake (200,100), NO, 0); CGContextRef con = UIGraphicsGetCurrentContext (); CGContextAddEllipseInRect (con, CGRectMake (0,0, 200,100); CGContextSetFillColorWithColor (con, [UIColor blueColor]. CGColor); CGContextFillPath (con); UIImage * img = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return img ;}
View Code

:

      

Bytes -----------------------------------------------------------------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.