quartz 2D 总结

来源:互联网 发布:程序员一年经验工资 编辑:程序博客网 时间:2024/06/10 06:36

基本步骤

1.获得图形上下文

CGContextRef ctx= UIGraphicsGetCurrentContext();

2.拼接路径(下面代码是搞一条线段)

CGContextMoveToPoint(ctx,10,10);

CGContextAddLineToPoint(ctx,100,100);

3.绘制路径    
CGContextStrokePath(ctx); //CGContextFillPath(ctx); (实心)


绘制线段     画矩形可以让线段变粗      设置起点 然后设置后面的点

    // Drawing code    // 1.获得图形上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();        // 2.拼接图形(路径)    // 设置线段宽度    CGContextSetLineWidth(ctx, 10);        // 设置线段头尾部的样式    CGContextSetLineCap(ctx, kCGLineCapRound);        // 设置线段转折点的样式    CGContextSetLineJoin(ctx, kCGLineJoinRound);        /**  第1根线段  **/    // 设置颜色    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);    // 设置一个起点    CGContextMoveToPoint(ctx, 10, 10);    // 添加一条线段到(100, 100)    CGContextAddLineToPoint(ctx, 100, 100);        // 渲染一次    CGContextStrokePath(ctx);            /**  第2根线段  **/    // 设置颜色    CGContextSetRGBStrokeColor(ctx, 0, 0, 1, 1);    // 设置一个起点    CGContextMoveToPoint(ctx, 200, 190);    // 添加一条线段到(150, 40)    CGContextAddLineToPoint(ctx, 150, 40);    CGContextAddLineToPoint(ctx, 120, 60);            // 3.渲染显示到view上面    CGContextStrokePath(ctx);


画矩形        设置颜色的时候  2中状态   空心与实心   简便办法就是颜色直接set

 // 1.获得上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();        // 2.画矩形    CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));        // set : 同时设置为实心和空心颜色    // setStroke : 设置空心颜色    // setFill : 设置实心颜色    [[UIColor whiteColor] set];    //    CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);        // 3.绘制图形    CGContextFillPath(ctx);


画图片

// 1.取得图片    UIImage *image = [UIImage imageNamed:@"me"];        // 2.画//    [image drawAtPoint:CGPointMake(50, 50)];             //    [image drawInRect:CGRectMake(0, 0, 150, 150)];    [image drawAsPatternInRect:CGRectMake(0, 0, 200, 200)];     平铺的画图       

画文字

 // 1.获得上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();    // 2.画矩形    CGRect cubeRect = CGRectMake(50, 50, 100, 100);          确认字放在规定的矩形中    CGContextAddRect(ctx, cubeRect);    // 3.显示所绘制的东西    CGContextFillPath(ctx);                // 4.画文字    NSString *str = @"哈哈哈哈Good morning hello hi hi hi hi";    //    [str drawAtPoint:CGPointZero withAttributes:nil];        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];    // NSForegroundColorAttributeName : 文字颜色    // NSFontAttributeName : 字体    attrs[NSForegroundColorAttributeName] = [UIColor redColor];    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:50];    [str drawInRect:cubeRect withAttributes:attrs];             <pre name="code" class="objc"><span style="white-space:pre"></span>withAttributes     放一个可变字典  




画圆弧


// 1.获得上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();        // 2.画圆弧    // x\y : 圆心    // radius : 半径    // startAngle : 开始角度    // endAngle : 结束角度    // clockwise : 圆弧的伸展方向(0:顺时针, 1:逆时针)    CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);            // 3.显示所绘制的东西    CGContextFillPath(ctx);



怎么样在画第一个结束  不影响第二个     

CGContextSaveGState(ctx);<pre name="code" class="objc">将ctx拷贝一份放到栈中            等于有2个存储点   这个存完以后 下一行代码销毁用过的备份

// 将栈顶的上下文出栈,替换当前的上下文    CGContextRestoreGState(ctx);



 // 1.获得上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();        // 将ctx拷贝一份放到栈中    CGContextSaveGState(ctx);        // 设置绘图状态    CGContextSetLineWidth(ctx, 10);    [[UIColor redColor] set];    CGContextSetLineCap(ctx, kCGLineCapRound);        // 第1根线    CGContextMoveToPoint(ctx, 50, 50);    CGContextAddLineToPoint(ctx, 120, 190);        CGContextStrokePath(ctx);        // 将栈顶的上下文出栈,替换当前的上下文    CGContextRestoreGState(ctx);            // 第2根线    CGContextMoveToPoint(ctx, 10, 70);    CGContextAddLineToPoint(ctx, 220, 290);        CGContextStrokePath(ctx);//    CGContextDrawPath(ctx, kCGPathStroke);


图形的缩放与移动(矩阵操作)

    CGContextRef ctx = UIGraphicsGetCurrentContext();            CGContextSaveGState(ctx);        CGContextRotateCTM(ctx, M_PI_4 * 0.3);    //在旋转    CGContextScaleCTM(ctx, 0.5, 0.5);<span style="white-space:pre"></span>//缩放    CGContextTranslateCTM(ctx, 0, 150);<span style="white-space:pre"></span>//上下移动        CGContextAddRect(ctx, CGRectMake(10, 10, 50, 50));        CGContextStrokePath(ctx);        CGContextRestoreGState(ctx);        CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 100, 100));    CGContextMoveToPoint(ctx, 100, 100);    CGContextAddLineToPoint(ctx, 200, 250);                CGContextStrokePath(ctx);

循环时间


    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];     默认1秒60次   适合1秒以下        //    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
//适合1秒以上




把某个控件单独拿出来用时   可以单独写入文件


CGContextRef ctx = UIGraphicsGetCurrentContext();        // 1.先创建一个路径    CGMutablePathRef linePath = CGPathCreateMutable();                // 2.拼接路径    CGPathMoveToPoint(linePath, NULL, 0, 0);    CGPathAddLineToPoint(linePath, NULL, 100, 100);        // 3.添加路径到上下文    CGContextAddPath(ctx, linePath);        CGMutablePathRef circlePath = CGPathCreateMutable();    CGPathAddArc(circlePath, NULL, 150, 150, 50, 0, M_PI * 2, 0);    CGContextAddPath(ctx, circlePath);        // 4.渲染    CGContextStrokePath(ctx);            CGPathRelease(linePath);    CGPathRelease(circlePath);            CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();    CGColorSpaceRelease(cs);


水印


+ (instancetype)waterImageWithBg:(NSString *)bg logo:(NSString *)logo{    UIImage *bgImage = [UIImage imageNamed:bg];        // 1.创建一个基于位图的上下文(开启一个基于位图的上下文)    UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);        // 2.画背景    [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];        // 3.画右下角的水印    UIImage *waterImage = [UIImage imageNamed:logo];    CGFloat scale = 0.2;     //比例    CGFloat margin = 5;<span style="white-space:pre"></span>/间距    CGFloat waterW = waterImage.size.width * scale;    CGFloat waterH = waterImage.size.height * scale;    CGFloat waterX = bgImage.size.width - waterW - margin;    CGFloat waterY = bgImage.size.height - waterH - margin;    [waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];        // 4.从上下文中取得制作完毕的UIImage对象    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();        // 5.结束上下文    UIGraphicsEndImageContext();        //begin开始 end结尾        return newImage;}

图片的裁剪

+ (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor{    // 1.加载原图    UIImage *oldImage = [UIImage imageNamed:name];        // 2.开启上下文    CGFloat imageW = oldImage.size.width + 2 * borderWidth;    CGFloat imageH = oldImage.size.height + 2 * borderWidth;    CGSize imageSize = CGSizeMake(imageW, imageH);    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);        // 3.取得当前的上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();        // 4.画边框(大圆)    [borderColor set];    CGFloat bigRadius = imageW * 0.5; // 大圆半径    CGFloat centerX = bigRadius; // 圆心    CGFloat centerY = bigRadius;    CGContextAddArc(ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0);    CGContextFillPath(ctx); // 画圆        // 5.小圆    CGFloat smallRadius = bigRadius - borderWidth;    CGContextAddArc(ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0);    // 裁剪(后面画的东西才会受裁剪的影响)    CGContextClip(ctx);        // 6.画图    [oldImage drawInRect:CGRectMake(borderWidth, borderWidth, oldImage.size.width, oldImage.size.height)];        // 7.取图    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();        // 8.结束上下文    UIGraphicsEndImageContext();        return newImage;}

屏幕截图


+ (instancetype)captureWithView:(UIView *)view{    // 1.开启上下文    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);        // 2.将控制器view的layer渲染到上下文    [view.layer renderInContext:UIGraphicsGetCurrentContext()];        // 3.取出图片    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();        // 4.结束上下文    UIGraphicsEndImageContext();        return newImage;}
- (IBAction)clip {    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        // 1.捕捉        UIImage *newImage = [UIImage captureWithView:self.view];                // 2.写文件        NSData *data = UIImagePNGRepresentation(newImage);        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];        [data writeToFile:path atomically:YES];    });}




0 0
原创粉丝点击