taiyang2014

Quartz 2D绘图

(1) 绘图Context是一个绘图的目标对象,定义了绘图的基本属性,如颜色、绘图范围、线宽及样式等。
 
   (2)通过UIView会创建Context,可以用类似如下的语句来得到当前的Context.
 
   CGContextRef currentContext = UIGraphicsGetCurrentContext();
 
   (3)如果在对其进行修改前想要保存当前状态,可以使用UIGraphicsPushContext;
 
         要恢复保存过的Context,则可用UIGraphicsPopContext。
 
   (4)path,路径其实就是用一系列坐标点标识出来的一条曲线或折线,创建好之后就可以沿其画线,或者对封闭的空间进行填充。
//重写初始化方法,在视图初始化的时候创建并设置自定义的Context
-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();//指定色彩空间为RGB
        whiteBoardContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8, 4 * self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
    }
   return self;
}
出现这个警告得处理方法:http://blog.csdn.net/thanklife/article/details/25790433
 
//
//  WhiteBoardView.m
//  draw2
//
//  Created by K-Net on 15-5-7.
//  Copyright (c) 2015年 K-Net. All rights reserved.
//

#import "WhiteBoardView.h"
//#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
//#define kCGImageAlphaPremultipliedLast (kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast)
//#else
//#define kCGImageAlphaPremultipliedLast  kCGImageAlphaPremultipliedLast
//#endif

@implementation WhiteBoardView
//重写初始化方法,在视图初始化的时候创建并设置自定义的Context
-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();//指定色彩空间为RGB
        whiteBoardContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8, 4 * self.frame.size.width, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
//        kCGBitmapAlphaInfoMask = 0x1F,
//        kCGBitmapFloatComponents = (1 << 8),
//        (CGBitmapInfo)kCGImageAlphaPremultipliedFirst
//        kCGBitmapByteOrderMask = 0x7000,
//        kCGBitmapByteOrderDefault = (0 << 12),
//        kCGBitmapByteOrder16Little = (1 << 12),
//        kCGBitmapByteOrder32Little = (2 << 12),
//        kCGBitmapByteOrder16Big = (3 << 12),
//        kCGBitmapByteOrder32Big = (4 << 12)
       
        CGColorSpaceRelease(colorSpace);
        //创建新的CGLayer,用于画图
        whiteBoardLayer =CGLayerCreateWithContext(whiteBoardContext, self.frame.size, NULL);
        //获得新建层的context
        CGContextRef layerContext = CGLayerGetContext(whiteBoardLayer);
        //指定新建层Context的线宽
        CGContextSetLineWidth(layerContext, 1.5);
        //指定线头形状为圆形
//        enum CGLineCap {
//            kCGLineCapButt,
//            kCGLineCapRound,
//            kCGLineCapSquare
//        };
        CGContextSetLineCap(layerContext, kCGLineCapRound);
        //指定线的颜色,黑色
        CGContextSetRGBStrokeColor(layerContext, 0.0, 0.7, 0.0, 1);
    }
    return self;
}
//重写drawRect方法
-(void)drawRect:(CGRect)rect {
    //先得到当前的Context
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    //根据Context的内容生成Bitmap
    CGImageRef image = CGBitmapContextCreateImage(currentContext);
    //把bitmap绘制到Context上
    CGContextDrawImage(currentContext, [self bounds], image);
    //把whiteBoardLayer也绘到当前Context上
    CGContextDrawLayerInRect(currentContext, [self bounds], whiteBoardLayer);
}

//屏幕触摸事件  触摸开始

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //得到touch对象
    UITouch *theTouch = [touches anyObject];
    if ([theTouch tapCount] == 2) {//如果是双击,就清空图像
        //CGContextClearRect清楚指定矩形区域上绘制的图形
        CGContextClearRect(whiteBoardContext, [self bounds]);//清空
        [self setNeedsDisplay];//刷新屏幕显示
    } else {
        [self touchesMoved:touches withEvent:event];
    }
}
//手指移动触发事件
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *theTouch = [touches anyObject];//获取任意手指触摸
    //获取当前的位置
    CGPoint currentTouchLocation = [theTouch locationInView:self];
    //得到上次的位置
    CGPoint lastTouchLocation = [theTouch previousLocationInView:self];
    //取得Context
    CGContextRef layerContext = CGLayerGetContext(whiteBoardLayer);
    //开始定义path
    CGContextBeginPath(layerContext);
    //把path的起点移动到上次位置
    CGContextMoveToPoint(layerContext, lastTouchLocation.x, lastTouchLocation.y);
    //在上次位置和当前位置之间连线,并计入path
    CGContextAddLineToPoint(layerContext, currentTouchLocation.x, currentTouchLocation.y);
    //沿path画线
    CGContextStrokePath(layerContext);
    [self setNeedsDisplay];//刷新屏幕显示
    NSLog(@"-================");
}
@end
 
 
 
 

posted on 2015-05-07 15:18  taiyang2014  阅读(144)  评论(0编辑  收藏  举报

导航