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;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容