iOS-图片倒圆角的三种方式

来源:互联网 发布:手机怎样备份所有数据 编辑:程序博客网 时间:2024/06/03 00:03

开发中,经常会遇到图片倒圆角,比如说用户图像等等,这里列举三种常用的方法,并分析各自的优缺点。


方法一:通过layer图层来设置图片圆角

    imageView.layer.cornerRadius = imageView.bounds.size.height * 0.5;    imageView.layer.masksToBounds = YES;

优点:方便快捷,适合单一的图片倒圆角,例如用户图像
缺点:耗性能


方法二:通过drawRect绘图设置图片圆角

- (UIImage *)clipImage {    // 01 开启图片上下文 第二个参数 NO 代表透明    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);    // 02 获得上下文    CGContextRef context = UIGraphicsGetCurrentContext();    // 03 添加一个圆    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);    CGContextAddEllipseInRect(context, rect);    // 04 剪切    CGContextClip(context);    // 05 将图片画上去    [self drawInRect:rect];    // 06 获取图片    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    // 07 关闭上下文    UIGraphicsEndImageContext();    // 08 返回图片    return image;}

优点:效率高,速度快,节省内存,适合多图片同时倒圆角,例如tableViewCell 内部图片倒圆角
缺点:代码量多,需要封装 或 给UIImage添加分类(category)


方法三:也是最省内存,最省时间,效率最高的一种方法

让美工给已经倒好圆角的图

这里优缺点就不说了,大家自己体会吧


总结:图片倒圆角的方法还有很多其他方法,我这里只总结出几种常用的方法,在现实开发中,具体用哪种方法比较好,大家可以自行选择。

0 0
原创粉丝点击