UIGraphics生成带边框的图片

实现思路

1、先生成一个矩形的UIBezierPath对象,设置该对象的一些属性等。用作外切的图片的边框。
2、再生成一个矩形的UIBezierPath对象,用作裁剪图片。
3、最后生成图片即可。

代码实例

+ (nonnull UIImage *)circleImageWithOriginImage:(UIImage *)image
                                    borderColor:(UIColor *)borderColor
                                    borderWidth:(CGFloat)borderWidth
                                         corner:(CGFloat)corner
                                      finalSize:(CGSize)finalSize
{
    CGFloat scale = [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContextWithOptions(finalSize, NO, scale);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, finalSize.width, finalSize.height) cornerRadius:corner];
    path.lineWidth = borderWidth;
    [borderColor set];
    [path addClip];
    [path fill];
    
    UIBezierPath *clicPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(borderWidth, borderWidth, finalSize.width - 2 * borderWidth, finalSize.height - 2 * borderWidth) cornerRadius:corner];
    [clicPath addClip];
    
    [image drawInRect:CGRectMake(borderWidth, borderWidth, finalSize.width - 2 * borderWidth, finalSize.height - 2 * borderWidth)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

你可能感兴趣的:(UIGraphics生成带边框的图片)