@implementation ZHCoreGView
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
[self drawMethod11];
}
//添加路径绘图
- (void)drawMethod11
{
// 1. 获取当前控件的图形上下文
// CG:表示这个类在CoreGraphics框架里 Ref:引用
CGContextRef context = UIGraphicsGetCurrentContext();
// 2. 描述绘画内容
// a. 创建图形路径
CGMutablePathRef path = CGPathCreateMutable();
// b. 创建图形起始点
CGPathMoveToPoint(path, NULL, 50, 50);
// c. 添加图形的终点 绘制直线
CGPathAddLineToPoint(path, NULL, 200, 50);
//CGPathAddQuadCurveToPoint(path, NULL, 250, 150, 0, 250); //绘制曲线
// 3. 把绘画内容添加到图形上下文
CGContextAddPath(context, path);
// 4. 设置图形上下文的状态(线宽、颜色等)
CGContextSetLineWidth(context, 5);
CGContextSetRGBStrokeColor(context, 0, 1, 0, 1);
// 5. 渲染图形上下文
CGContextStrokePath(context);
}
//使用上下文直接绘图
- (void)drawMethod12
{
// 1. 获取当前控件的图形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 2. 描述绘画内容
// a. 创建图形起始点
CGContextMoveToPoint(context, 20, 50);
// b. 添加图形的终点
CGContextAddLineToPoint(context, 200, 150);
CGContextAddLineToPoint(context, 200, 55);
// 绘制曲线 添加控制点和终点,控制点(300,200),终点(0,250)
// CGContextAddQuadCurveToPoint(context, 300, 200, 0, 250);
// 3. 设置图形上下文的状态(线宽、颜色等)
CGContextSetLineWidth(context, 5);
CGContextSetCMYKStrokeColor(context, 0, 1, 1, 0, 1);
// 4. 渲染图形上下文
CGContextStrokePath(context);
}
//贝瑟尔路径(UIBezierPath)绘图
- (void)drawBezier
{
// 1. 创建贝瑟尔路径
UIBezierPath *path = [UIBezierPath bezierPath];
// 2. 设置起点
[path moveToPoint:CGPointMake(20, 20)];
// 3. 设置终点
[path addLineToPoint:CGPointMake(80, 150)];
// 4. 设置路径状态
// 设置颜色
[[UIColor redColor] set];
// 设置线宽
[path setLineWidth:5];
// 4. 绘制路径
[path stroke];
}
//测试 CGContextSaveGState CGContextRestoreGState
//CGContextSaveGState函数的作用是将当前图形状态推入堆栈。之后,您对图形状态所做的修改会影响随后的描画操作,但不影响存储在堆栈中的拷贝。在修改完成后,您可以通过CGContextRestoreGState函数把堆栈顶部的状态弹出,返回到之前的图形状态。这种推入和弹出的方式是回到之前图形状态的快速方法,避免逐个撤消所有的状态修改;这也是将某些状态(比如裁剪路径)恢复到原有设置的唯一方式。
- (void)TestCGContextSaveGStateCGContextRestoreGState
{
// 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 描述图形内容
// 第一根线
UIBezierPath *path1 = [UIBezierPath bezierPath];
[path1 moveToPoint:CGPointMake(50, 10)];
[path1 addLineToPoint:CGPointMake(50, 150)];
// 把第一根添加到上下文
CGContextAddPath(context, path1.CGPath);
// 保存一份初始的上下文状态
CGContextSaveGState(context);
// 设置上下文状态
[[UIColor redColor] set];
CGContextSetLineWidth(context, 5);
// 渲染上下文
CGContextStrokePath(context);
// 第二根线
UIBezierPath *path2 = [UIBezierPath bezierPath];
[path2 moveToPoint:CGPointMake(100, 10)];
[path2 addLineToPoint:CGPointMake(100, 150)];
// 添加到上下文
CGContextAddPath(context, path2.CGPath);
// 还原上下文状态
CGContextRestoreGState(context);
// 渲染上下文
CGContextStrokePath(context);
}
//图片加文字水印,添加图片水印类似
+ (UIImage *)zh_WaterImageWithImage:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary * )attributed{
//1.开启上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//2.绘制图片
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
//添加水印文字
[text drawAtPoint:point withAttributes:attributed];
//3.从上下文中获取新图片
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//4.关闭图形上下文
UIGraphicsEndImageContext();
//返回图片
return newImage;
}
//view 生成图片(也可作为截屏)
+(UIImage*)imageFromView:(UIView *)view {
UIImage *result;
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
//图片裁剪
+ (UIImage*)cutImage
{
// 创建图片
UIImage *image = [UIImage imageNamed:@"touxiang"];
// 1. 开启上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
// 2. 设置裁剪区
// 创建图形路径
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
// 把图形路径设置为裁剪区
[path addClip];
// 3. 绘制图形
[image drawAtPoint:CGPointZero];
// 4. 从位图上下文获取图片
UIImage *cutImage = UIGraphicsGetImageFromCurrentImageContext();
// 5. 关闭上下文
UIGraphicsEndImageContext();
return cutImage;
}
@end
CoreGraphics绘图
最新推荐文章于 2022-03-12 23:25:37 发布