美文网首页
给image添加边框

给image添加边框

作者: th先生 | 来源:发表于2022-02-16 16:48 被阅读0次
+ (UIImage *)imageWithBorder:(CGFloat)borderW image:(NSString *)imageName {
   
    // 增加边框 生成边框的宽度 w = image.width + 2*borderW 高度同理
    UIImage *image = [UIImage imageNamed:imageName];
   
    // 开启上下文
    CGSize size = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 * borderW);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
   
    // 绘制大圆,显示出来
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
    [[UIColor clearColor] set];
    [path fill];
   
    // 绘小圆
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];
    // 设置为裁剪路径
    [clipPath addClip];
   
    // 画图
    [image drawAtPoint:CGPointMake(borderW, borderW)];
    UIImage *newImage =  UIGraphicsGetImageFromCurrentImageContext();
   
    UIGraphicsEndImageContext();
   
    return newImage;
   
}

相关文章

网友评论

      本文标题:给image添加边框

      本文链接:https://www.haomeiwen.com/subject/mkvjhrtx.html