您的位置:首页 > 其它

Quartz 2D绘图

2015-05-12 16:58 323 查看
- (void)drawRect:(CGRect)rect {

    

    CGContextRef content =
UIGraphicsGetCurrentContext();

    

    //1.绘制图画

//    [self drawImage:content];

    

    //2.绘制文字

//    [self drawtext:content];

    

    //3.绘制贝塞尔曲线

//    [self drawCure:content];

    

    //4.绘制圆弧
    [self
drawArc:content];
}

#pragma mark - 绘制图画
- (void)drawImage:(CGContextRef)content {

    UIImage *image = [UIImage
imageNamed:@"2012100413195471481.jpg"];

    

    //转换坐标

    CGContextSaveGState(content);

    CGContextRotateCTM(content,
M_PI);
   
CGContextScaleCTM(content, -1,
1);
   
CGContextTranslateCTM(content,
0, -self.bounds.size.height);

    
   
CGContextDrawImage(content,
self.bounds, image.CGImage);

    

    CGContextRestoreGState(content);

}

#pragma mark - 绘制文字
- (void)drawtext:(CGContextRef)content {
   
NSString *str = @"哈哈哈";

    
   
CGRect rect = CGRectMake(50,
50, 200,
200);

    [[UIColor
redColor] setFill];
   
UIRectFill(rect);

    
   
CGRect rect1 = CGRectMake(200,
50, 200,
200);

    [[UIColor
orangeColor] setFill];
   
UIRectFill(rect1);

    

    
   
UIFont *font = [UIFont
systemFontOfSize:20];

    [str drawInRect:rect
withAttributes:@{NSFontAttributeName:font}];

    

    
}

#pragma mark - 绘制贝塞尔曲线
- (void)drawCure:(CGContextRef)context {

    CGContextSetLineWidth(context,
5);

    [[UIColor
redColor] setStroke];

    

    CGContextMoveToPoint(context,
20,
100);

    
   
CGContextAddCurveToPoint(context,
100, 20, 200,
300, 300,
50);

    

    CGContextDrawPath(context,
kCGPathStroke);
}

#pragma mark - 绘制圆弧
- (void)drawArc:(CGContextRef)context {

    

    //转换坐标

    CGContextSaveGState(context);

    CGContextRotateCTM(context,
M_PI);
   
CGContextScaleCTM(context, -1,
1);
   
CGContextTranslateCTM(context,
0, -self.bounds.size.height);

    

    [[UIColor
grayColor] setFill];

    [[UIColor
redColor] setStroke];
   
CGPoint centerPoint =
CGPointMake(150,
150);
   
float radius = 100;
   
CGFloat beginAngle =
0;
   
CGFloat endAngle =
M_PI_4;
   
CGContextAddArc(context, centerPoint.x, centerPoint.y, radius, beginAngle, endAngle,
1);

    
   
CGContextMoveToPoint(context, centerPoint.x+radius*cos(beginAngle), centerPoint.y+radius*sin(beginAngle));
   
CGContextAddLineToPoint(context, centerPoint.x, centerPoint.y);
   
CGContextAddLineToPoint(context, centerPoint.x+radius*cos(endAngle), centerPoint.y+radius*sin(endAngle));

    

    CGContextDrawPath(context,
kCGPathEOFillStroke);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: