01.绘制线条的第一种方法,最基本的方式
// 01.获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 02.创建可变路径
CGMutablePathRef mutablePathRef = CGPathCreateMutable();
// 03.移动到起始点
CGPathMoveToPoint(mutablePathRef, NULL, 50, 50);
// 04.添加线
CGPathAddLineToPoint(mutablePathRef, NULL, 200, 200);
// 05.给上线文添加线
CGContextAddPath(context, mutablePathRef);
// 06.绘制
CGContextStrokePath(context);
02.绘制线条的第二种方法
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 200, 200);
CGContextStrokePath(context);
03.绘制一条直线的第三种方式,BezierPath曲线
// 绘制一条直线的第三种方式,BezierPath曲线
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(200, 200)];
[path stroke];
04.图形上下文状态栈
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 第一条直线
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(10, 120)];
[bezierPath addLineToPoint:CGPointMake(200, 120)];
// bezierPath.lineWidth = 10;
// bezierPath.lineCapStyle = kCGLineCapRound;
// 存储绘制状态
CGContextSaveGState(ctx);
[[UIColor redColor] set];
CGContextSetLineWidth(ctx, 10);
// 把贝塞尔路径转成上下文路径
CGContextAddPath(ctx, bezierPath.CGPath);
// 使用上线文绘制直线
CGContextStrokePath(ctx);
// [bezierPath stroke];
// 第二条直线
bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(120, 10)];
[bezierPath addLineToPoint:CGPointMake(120, 200)];
// [[UIColor blackColor] set];
// CGContextSetLineWidth(ctx, 1);
// 把贝塞尔路径转成上下文路径
CGContextAddPath(ctx, bezierPath.CGPath);
// 还原状态
CGContextRestoreGState(ctx);
// 使用上线文绘制直线
CGContextStrokePath(ctx);
// [bezierPath stroke];
}
05.上下文矩阵操作
// 上下文矩阵操作
- (void)matrixWithContext:(CGContextRef)ctx {
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
// 上下文矩阵操作,必须放在添加路径之前
CGContextTranslateCTM(ctx, 100, 50);
// 缩放
CGContextScaleCTM(ctx, 0.5, 0.5);
// 旋转
CGContextRotateCTM(ctx, M_PI_4);
// 添加路径
CGContextAddPath(ctx, path.CGPath);
[[UIColor redColor] set];
CGContextFillPath(ctx);
// [[UIColor redColor] set];
// [path fill];
}
06.生成带水印的图片
#import "UIImage+Extention.h"
@implementation UIImage (Extention)
/** 生成带有水印的图片 */
- (UIImage *)imageAddWatermarkWithText:(NSString *)text
withAttributes:(NSDictionary *)attrs
drawInPoint:(CGPoint)point{
// 01.获取图片绘制上下文
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0);
// 02.绘制图片
[self drawAtPoint:CGPointZero];
// 03.绘制文字
[text drawAtPoint:point withAttributes:attrs];
UIImage *waterImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return waterImage;
}
@end
07.通过路径裁剪图片
/**
* 通过路径裁剪图片
*
* @param path 图片的裁剪路径(UIBezierPath)
*
* @return 被裁剪的图片
*/
- (UIImage *)imageClipWithPath:(UIBezierPath *)path {
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0);
[path addClip];
[self drawAtPoint:CGPointZero];
UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return clipImage;
}
08.通过已有图片生成带圆环的图片
/**
* 通过已有图片生成带圆环的图片
*
* @param border 圆环宽度
* @param borderColor 圆环颜色
*
* @return 带圆环的图片
*/
- (UIImage *)imageClipWithRingBorder:(CGFloat)border borderColor:(UIColor *)borderColor {
CGFloat imageW = self.size.width;
CGFloat imageH = self.size.height;
CGFloat ovalW = imageW + 2 * border;
CGFloat ovalH = imageH + 2 * border;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovalW, ovalH), NO, 0);
UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalW, ovalH)];
[borderColor set];
[ovalPath fill];
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageW, imageH)];
[clipPath addClip];
[self drawAtPoint:CGPointMake(border, border)];
UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return clipImage;
}
- 视图截图
/**
* 获取视图的截图
*
* @return 截图
*/
- (UIImage *)imageByViewshots {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.layer renderInContext:ctx];
UIImage *screenshots = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenshots;
}
10.擦除图片例子
- (void)viewDidLoad {
[super viewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan {
CGPoint curP = [pan locationInView:self.view];
CGFloat wh = 30;
CGFloat x = curP.x - wh * 0.5;
CGFloat y = curP.y - wh * 0.5;
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.imgView.layer renderInContext:ctx];
// 擦除上下文内部区域
CGContextClearRect(ctx, CGRectMake(x, y, wh, wh));
self.imgView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}