OC圆角图片绘制

0.方法零
不推荐使用系统圆角属性设置image圆角。量多时容易导致一些性能问题
1.方法一(有用送颗❤)

- (UIImage *)circleImage:(UIImage *)image{
    
    // NO代表透明度
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 1.0);
    
    // 获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 添加一个圆
    CGRect rect2 = CGRectMake(0, 0, image.size.width, image.size.height);
    
    CGContextAddEllipseInRect(ctx, rect2);
    
    // 裁剪
    CGContextClip(ctx);
    
    // 将图片画上去
    [image drawInRect:rect2];
    
    UIImage *image2 = UIGraphicsGetImageFromCurrentImageContext();
    
    // 关闭上下文
    UIGraphicsEndImageContext();
    
    return image2;
    
}

2.方法二(有用送颗❤)

- (UIImage *)roundImageClip:(CGRect)rect image:(UIImage *)image{
    // NO代表透明度
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0);

    创建贝萨尔曲线画内切圆
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:50];
    // 设置填充颜色
     [fillColor setFill];
     UIRectFill(rect);
 
    // 裁剪
    [path addClip];

    // 将图片画上去
    [image drawInRect:rect];

    // 获取裁剪后的图片
    UIImage *iage = UIGraphicsGetImageFromCurrentImageContext();

    关闭上下文
    UIGraphicsEndImageContext();
    return iage;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容