Quartz2D——旋转、平移、缩放、剪切圆形图片

注:旋转、平移、缩放,必须放在画图之前

平移:

<span style="font-size:18px;">// 获取上下文
    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);
</span>

效果:



旋转:围绕左上角(00)旋转

 // 获取上下文
    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);

效果:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值