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);
}
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);
}
相关文章推荐
- quartz 2d 绘图 工具箱
- iOS开发UI篇—Quartz2D使用(绘图路径)
- QUARTZ-2D绘图之图形上下文详解
- iOS 2D 绘图(Quartz2D)
- Quartz2D绘图的基本使用
- iOS笔记052- Quartz2D-绘图
- UI高级----------- Quartz 2D 绘图
- iOS高级-QuartzCore框架-2D绘图
- iOS开发学习之Quartz2D绘图
- iOS-绘图(Quartz2D)的简单使用(原创)
- iOS:quartz2D绘图(处理图像,绘制图像并添加水印)
- Quartz2D绘图之利用路径绘制复杂图形
- iOS 2D绘图详解(Quartz 2D)之路径(stroke,fill,clip,subpath,blend)
- iOS 2D绘图详解(Quartz 2D)之路径(stroke,fill,clip,subpath,blend)
- 第三十四篇:Quartz2D绘图
- Quartz2D绘图
- Quartz-2D绘图之路径(Paths)详解
- iOS Quartz 2D绘图用CGContextRef绘制三角形 —— HERO博客
- iOS开发UI篇—Quartz2D(五) 图片裁剪圆形,UIKit的绘图方法,Path路径的使用
- iOS开发UI篇—Quartz2D使用(绘图路径)