XMG Quartz2D 设置裁剪区域 最外面有圆环

1.

唯一的区别就是在上一篇的基础上一开始绘制了一个填充的圆。画布的大小比刚才的裁剪区域大,这样就会在最外面有一圈圆环


//源码

#import "UIImage+Image.h"


@implementation UIImage (Image)

+ (UIImage *)imageWithClipImage:(UIImage *)image borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)color

{

    // 图片的宽度和高度

    CGFloat imageWH = image.size.width;

    

    // 设置圆环的宽度

    CGFloat border = borderWidth;

    

    // 圆形的宽度和高度

    CGFloat ovalWH = imageWH + 2 * border;

    

    // 1.开启上下文

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovalWH, ovalWH), NO, 0);

    

    // 2.画大圆

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalWH, ovalWH)];

    

    [color set];

    

    [path fill];

    

    // 3.设置裁剪区域

    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH, imageWH)];

    [clipPath addClip];

    

    // 4.绘制图片

    [image drawAtPoint:CGPointMake(border, border)];

    

    // 5.获取图片

    UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();

    

    // 6.关闭上下文

    UIGraphicsEndImageContext();

    

    return clipImage;

    

}



你可能感兴趣的:(XMG Quartz2D 设置裁剪区域 最外面有圆环)