zoukankan      html  css  js  c++  java
  • 基本绘图的几种方式

    1. drawRect:

      UIView子类重写

    2. drawLayer: inContext:

      CALayer设置代理 (这是个代理方法)

    3. drawInContext:

      CALayer子类重写

    4. 使用图形上下文生成图片:

      imageContext

     尽量避免混用

                        -------实现 drawRect : 方法----------

       1、使用 UIKit      

          
            /**
                1、UIView子类实现 drawRect: 方法
             
                2、在 UIKit 提供的当前上下文中绘制
             */
            - (void)drawRect:(CGRect)rect {
                
                // 贝瑟尔路径描绘椭圆
                UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)];
                
                // 颜色填充
                [[UIColor blueColor] setFill];
                
                // 填充到当前上下文
                [path fill];
    }
    View Code

         效果图:

          

       2、使用Core Graphic

         
            /**
                1、UIView子类实现 drawRect: 方法
             
                2、使用 Core Graphics 获得当前上下文进行绘制
             */
            - (void)drawRect:(CGRect)rect {
                
                // 获取当前上下文
                CGContextRef context = UIGraphicsGetCurrentContext();
                
                // 绘制图形
                CGContextAddEllipseInRect(context, CGRectMake(100, 100, 200, 100));
                
                // 设置填充颜色
                CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
                
                // 渲染
                CGContextFillPath(context);
            }
    View Code

        效果图:

          

    -----------------------------------------------------------------------------------------------

                    -------代理实现 drawLayer: inContext: 方法----------

      1、使用UIKit

          
                @implementation ViewController
                
                - (void)viewDidLoad{
                     [super viewDidLoad];
                
                     //1.创建自定义的layer
                     CALayer *layer=[CALayer layer];
                
                     //2.设置layer的属性
                     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;
                
                     //设置代理
                     layer.delegate=self;
                
                    // 触发代理方法进行绘制
                     [layer setNeedsDisplay];
                
                     //3.添加layer
                     [self.view.layer addSublayer:layer];
                 }
                
                -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
                    
                    // 将引用的上下文转变成当前上下文。
                    UIGraphicsPushContext(ctx);
                    
                    // 绘制图形
                    UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50,50,100,50)];
                    
                    [[UIColor blueColor] setFill];
                    
                    [p fill];
                    
                    
                    UIGraphicsPopContext();
                }
                
                @end
    View Code

       效果图:

          

      2、使用Core Graphic

          
                @implementation ViewController
                
                - (void)viewDidLoad{
                     [super viewDidLoad];
                
                     //1.创建自定义的layer
                     CALayer *layer=[CALayer layer];
                
                     //2.设置layer的属性
                     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;
                
                     //设置代理
                     layer.delegate=self;
                     [layer setNeedsDisplay];
                     //3.添加layer
                     [self.view.layer addSublayer:layer];
                 }
                
                -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
                    
                    CGContextAddEllipseInRect(ctx, CGRectMake(50,50,100,50));
                    
                    CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
                    
                    CGContextFillPath(ctx);
                }
                
                @end
    View Code

        效果图:

          

    -----------------------------------------------------------------------------------------------

                    -------自定义CALayer 重写 drawInContext:----------

      1、使用UIKit 

          自定义Layer类:

            
                    #import "DXLayer.h"
                    #import <UIKit/UIKit.h>
                    
                    @implementation DXLayer
                    
                    //重写该方法,在该方法内绘制图形
                    -(void)drawInContext:(CGContextRef)ctx{
                        
                        UIGraphicsPushContext(ctx);
                        
                        //1.绘制图形
                        UIBezierPath*p =[UIBezierPath bezierPathWithOvalInRect:CGRectMake(50,50,100,50)];
                    
                        [[UIColor blueColor] setFill];
                    
                        [p fill];
                        
                        UIGraphicsPopContext();
                    }
                    
                    @end
    View Code

          使用:

            
                    @implementation ViewController
                    
                    - (void)viewDidLoad{
                        [super viewDidLoad];
                        
                         //1.创建自定义的layer
                         DXLayer *layer=[DXLayer layer];
                        
                         //2.设置layer的属性
                         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.触发layer的 drawInContext: 方法 在自定义Layer上进行绘制
                         [layer setNeedsDisplay];
                        
                         //4.添加layer
                         [self.view.layer addSublayer:layer];
                     }
                    
                    @end
                    
    View Code

        效果图:

          

      2、使用Core Graphic

          
                    #import "DXLayer.h"
                    
                    @implementation DXLayer
                    
                    //重写该方法,在该方法内绘制图形
                    -(void)drawInContext:(CGContextRef)ctx{
                        
                         //1.绘制图形
                         //画一个圆
                         CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 100, 100));
                        
                         //设置属性(颜色)
                         // [[UIColor yellowColor]set];
                         CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
                    
                         //2.渲染
                         CGContextFillPath(ctx);
                     }
                    @end
    View Code

        效果图:

          

    -----------------------------------------------------------------------------------------------

                    -------使用 Image Context----------

        1、通过 ImageContext 生成一个 UIImage对象

          2、代码位置无限制  以上两种方式都限制在 drawRect:  或  drawLayer: inContext: 方法中

     

      1、使用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];
                    
                 }
                
                /** 通过绘图的方式 生成图片对象 (画个圈圈) */
                - (UIImage *) createImageFromImageContext{
                    
                    /**
                        创建图形上下文
                     
                            1: 所要创建的图片的尺寸
                     
                            2: 指定所生成图片的背景是否为不透明
                     
                            3: 指定生成图片的缩放因子,与UIImage的scale属性所指的含义是一致的。
                     
                                    传入0则表示让图片的缩放因子根据屏幕的分辨率而变化
                     */
                    UIGraphicsBeginImageContextWithOptions(CGSizeMake(200,100), NO, 0);
                    
                    // UIKit 进行绘图
                    UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,200,100)];
                    
                    [[UIColor blueColor] setFill];
                    
                    [p fill];
                    
                    // 从上下文中获取图片对象
                    UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
                    
                    // 关闭图形上下文
                    UIGraphicsEndImageContext();
                    
                    return img;
                }
                
    @end
    View Code

        效果图:

          

      2、使用Core Graphic

          
                /** 通过绘图的方式 生成图片对象 (画个圈圈) */
                - (UIImage *) createImageFromImageContext{
                    
                    /**
                        创建图形上下文
                     
                            1: 所要创建的图片的尺寸
                     
                            2: 指定所生成图片的背景是否为不透明
                     
                            3: 指定生成图片的缩放因子,与UIImage的scale属性所指的含义是一致的。
                     
                                    传入0则表示让图片的缩放因子根据屏幕的分辨率而变化
                     */
                    UIGraphicsBeginImageContextWithOptions(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

        效果图:

          

    -----------------------------------------------------------------------------------------------

  • 相关阅读:
    AngularJS:添加检查密码输入是否一致的功能
    Clojure:两步发送iOS推送通知(apns)
    Openfire:解决乱码问题
    iOS:让UIView覆盖导航栏
    python 图片压缩存储
    jQuery 树形结构
    jsTree
    Mrakdown文本编辑器
    Flask jQuery ajax
    flask-sqlalchemy分表解决方案
  • 原文地址:https://www.cnblogs.com/dx-230/p/7150440.html
Copyright © 2011-2022 走看看