注:旋转、平移、缩放,必须放在画图之前
平移:
// 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 画一个三角形
// 定义三个点
CGPoint point[3] = {{50, 50}, {100, 80}, {10, 80}};
CGContextAddLines(context, point, 3);
// 合并路径
CGContextClosePath(context);
// 渲染
CGContextStrokePath(context);
#warning 旋转、平移、缩放,都要放在画图之前
// 平移
CGContextTranslateCTM(context, 100, 0);
// 画第二个三角形
// 定义三个点
CGPoint points[3] = {{50, 50}, {100, 80}, {10, 80}};
CGContextAddLines(context, points, 3);
[[UIColor whiteColor] set];
// 合并路径
CGContextClosePath(context);
// 渲染
CGContextStrokePath(context);
旋转:围绕左上角(0,0)旋转
// 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 画一个三角形
// 定义三个点
CGPoint point[3] = {{50, 50}, {100, 80}, {10, 80}};
CGContextAddLines(context, point, 3);
// 合并路径
CGContextClosePath(context);
// 渲染
CGContextStrokePath(context);
// 旋转
// 负数:逆时针
// 围绕左上角(0,0)旋转
CGContextRotateCTM(context, - M_PI * 0.125);
// 画第二个三角形
// 定义三个点
CGPoint points[3] = {{50, 50}, {100, 80}, {10, 80}};
CGContextAddLines(context, points, 3);
[[UIColor whiteColor] set];
// 合并路径
CGContextClosePath(context);
// 渲染
CGContextStrokePath(context);
效果:
缩放:
// 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 画一个三角形
// 定义三个点
CGPoint point[3] = {{50, 50}, {100, 80}, {10, 80}};
CGContextAddLines(context, point, 3);
// 合并路径
CGContextClosePath(context);
// 渲染
CGContextStrokePath(context);
// 缩放
CGContextScaleCTM(context, 3, 4);
// 画第二个三角形
// 定义三个点
CGPoint points[3] = {{50, 50}, {100, 80}, {10, 80}};
CGContextAddLines(context, points, 3);
[[UIColor whiteColor] set];
// 合并路径
CGContextClosePath(context);
// 渲染
CGContextStrokePath(context);
效果:
裁剪圆形图片:
// 获取图形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置图片的rect
CGRect imageRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
// 在rect中画一个内切圆
CGContextAddEllipseInRect(context, imageRect);
// 剪掉圆外面的部分
CGContextClip(context);
// 只把图片显示在UIView上
UIImage *image = [UIImage imageNamed:@"papa"];
[image drawInRect:imageRect];
// 添加一个圆形的边框
// 设置边框颜色
[[UIColor blueColor] set];
// 设置线宽
CGContextSetLineWidth(context, 5);
// 画圆
CGContextAddEllipseInRect(context, imageRect);
// 渲染
CGContextStrokePath(context);