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

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

平移:

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

效果:

Quartz2D——旋转、平移、缩放、剪切圆形图片_第1张图片


旋转:围绕左上角(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);
效果:

Quartz2D——旋转、平移、缩放、剪切圆形图片_第2张图片

缩放:

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

Quartz2D——旋转、平移、缩放、剪切圆形图片_第3张图片

裁剪圆形图片:

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

效果:

Quartz2D——旋转、平移、缩放、剪切圆形图片_第4张图片





你可能感兴趣的:(iPhone开发,quartz2D旋转平移缩放,将图片剪切为圆形)