UIImageView圆角设置方法优化

一般设置圆角的代码

self.iconImage.layer.cornerRadius = 20;

self.iconImage.layer.masksToBounds = YES;

这样被切除的部分会带有alpha透明度,可以使用模拟器检测出来!(检测方法:模拟器——>Debug——>Color Blended Layers        检查项目中有透明度的ui(如果带有alpha透明度,显示酒红色,需要优化))如下图:

UIImageView圆角设置方法优化_第1张图片

主要看头像哪里,途中好多label也是红色,因为uilabel默认是透明的,一般的项目中除了uilabel这个ui空间可以是透明的,其他的控件都将其设置为不透明(透明会增加两个甚至多个空间的颜色叠加计算)。

在此之后建议大家尽量不要这么设置, 因为使用图层过量会有卡顿现象, 特别是弄圆角或者阴影会很卡, 如果设置图片圆角我们一般用绘图来做:

/** 设置圆形图片(放到分类中使用) */

- (UIImage *)cutCircleImage {

UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);

// 获取上下文

CGContextRef ctr = UIGraphicsGetCurrentContext();

// 设置圆形

CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);

CGContextAddEllipseInRect(ctr, rect);

// 裁剪

CGContextClip(ctr);

// 将图片画上去

[self drawInRect:rect];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

这个方法就是设置圆角图片, 效率很高, 不会造成卡顿现象, 大家要把这个方法单独放到分类中使用

转自:http://www.cocoachina.com/ios/20160819/17398.html

你可能感兴趣的:(UIImageView圆角设置方法优化)