CoreGraphics绘图

本文详细介绍iOS平台上的图形绘制方法,包括使用Core Graphics框架进行基本绘图操作,如线条、曲线的绘制,以及如何利用CGContextSaveGState和CGContextRestoreGState进行图形状态管理。此外,还介绍了如何通过UIBezierPath实现复杂图形绘制,并提供了图片加文字水印、截图生成图片等功能的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@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




                
AppWizard has created this drawGraphics application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your drawGraphics application. drawGraphics.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. drawGraphics.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CDrawGraphicsApp application class. drawGraphics.cpp This is the main application source file that contains the application class CDrawGraphicsApp. drawGraphics.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. drawGraphics.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. resdrawGraphics.ico This is an icon file, which is used as the application s icon. This icon is included by the main resource file drawGraphics.rc. resdrawGraphics.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值