Core Graphics 学习笔记 之 Image Drawing(二)

1.绘制箭头的三种方式

  // Drawing code
//    CGContextRef con = UIGraphicsGetCurrentContext();
//    //draw a black(by default) vertical line,the shaft of the arrow  箭杆
//    CGContextMoveToPoint(con, 100, 100);
//    CGContextAddLineToPoint(con, 100, 19);
//    CGContextSetLineWidth(con, 20);
//    CGContextStrokePath(con);
//    //draw a red triangle,the point of the arrow
//    CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
//    CGContextMoveToPoint(con, 80, 25);
//    CGContextAddLineToPoint(con, 100, 0);
//    CGContextAddLineToPoint(con, 120, 25);
//    CGContextFillPath(con);
//    //snip a triangle out of the shaft by drawing in clear blend mode
//    CGContextMoveToPoint(con, 90, 100);
//    CGContextMoveToPoint(con, 100, 90);
//    CGContextMoveToPoint(con, 110, 100);
//    CGContextSetBlendMode(con, kCGBlendModeClear);
//    CGContextSetAlpha(con, 1.0);
//    CGContextFillPath(con);

     //当需要获取CGPath时,可以使用UIBezierPath
//    UIBezierPath *p = [UIBezierPath bezierPath];
//    
//    [p moveToPoint:CGPointMake(100, 100)];
//    [p addLineToPoint:CGPointMake(100, 19)];
//    [p setLineWidth:20];
//    [p stroke];
//    [[UIColor redColor] set];
//    [p removeAllPoints];
//    [p moveToPoint:CGPointMake(80, 25)];
//    [p addLineToPoint:CGPointMake(100, 0)];
//    [p addLineToPoint:CGPointMake(120, 25)];
//    [p fill];
//    
//    [p removeAllPoints];
//    [p moveToPoint:CGPointMake(90, 101)];
//    [p addLineToPoint:CGPointMake(100, 90)];
//    [p addLineToPoint:CGPointMake(110, 101)];
//    [p fillWithBlendMode:kCGBlendModeClear alpha:1.0];
    
   //Clipping
    CGContextRef con = UIGraphicsGetCurrentContext();
    //punch triangular hole in context clipping region
    CGContextMoveToPoint(con, 90, 100);
    CGContextAddLineToPoint(con, 100, 90);
    CGContextAddLineToPoint(con, 110, 100);
    CGContextClosePath(con);
    CGContextAddRect(con, CGContextGetClipBoundingBox(con));
    CGContextEOClip(con);
    //draw the vertical line
    CGContextMoveToPoint(con, 100, 100);
    CGContextAddLineToPoint(con, 100, 19);
    CGContextSetLineWidth(con, 20);
    CGContextStrokePath(con);
    //draw the red triangle.the point of the arrow
    CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
    CGContextMoveToPoint(con, 80, 25);
    CGContextAddLineToPoint(con, 100, 0);
    CGContextAddLineToPoint(con, 120, 25);
    CGContextFillPath(con);
效果:









    CGContextRef con = UIGraphicsGetCurrentContext();
    [[self arrowImage]  drawAtPoint:CGPointMake(0, 0)];
    for (int i=0; i<3; i++) {
        CGContextTranslateCTM(con, 20, 100);
        CGContextRotateCTM(con, 30*M_PI/180);
        CGContextTranslateCTM(con, -20, -100);
        [[self arrowImage] drawAtPoint:CGPointMake(0, 0)];
        
    }
//
}

-(UIImage *)arrowImage
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 100), NO, 0.0);
    CGContextRef con = UIGraphicsGetCurrentContext();
    //punch triangular hole in context clipping region
    CGContextMoveToPoint(con, 10, 100);
    CGContextAddLineToPoint(con, 20, 90);
    CGContextAddLineToPoint(con, 30, 100);
    CGContextClosePath(con);
    CGContextAddRect(con, CGContextGetClipBoundingBox(con));
    CGContextEOClip(con);
    //draw the vertical line
    CGContextMoveToPoint(con, 20, 100);
    CGContextAddLineToPoint(con, 20, 19);
    CGContextSetLineWidth(con, 20);
    CGContextStrokePath(con);
    //draw the red triangle.the point of the arrow
    CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
    CGContextMoveToPoint(con, 0, 25);
    CGContextAddLineToPoint(con, 20, 0);
    CGContextAddLineToPoint(con, 40, 25);
    CGContextFillPath(con);
    
    UIImage *im = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return im;
}






当我不是绘制在(0,0)时就有问题,好像是由坐标系统改变导致的

    CGContextRef con = UIGraphicsGetCurrentContext();
    [[self arrowImage]  drawAtPoint:CGPointMake(150, 200)];
    for (int i=0; i<12; i++) {
        CGContextTranslateCTM(con, 20, 100);
        CGContextRotateCTM(con, 30*M_PI/180);
        CGContextTranslateCTM(con, -20, -100);
        [[self arrowImage] drawAtPoint:CGPointMake(150, 200)];
        
    }


尚未找到原因,有知道的童鞋,欢迎告知,一起交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值