PS:記錄自己工作學習中的一些知識;
一、UIImageView
- iOS9之后:
UIImageView使用以下方法是不會觸發(fā)離屏渲染,所以如果不適配iOS9以前的版本得話,大可放心使用;
imageView.layer.cornerRadius = 30;
imageView.layer.masksToBounds = YES;
- 適配iOS9之前的版本建議使用以下方法,使用CPU渲染,渲染得到的bitmap最后再交由GPU用于顯示,得到的結果是一樣的;
/**
* On-screen-renderring繪制UIImage矩形圓角
*/
- (UIImage *)imageWithCornerRadius:(CGFloat)radius ofSize:(CGSize)size{
/* 當前UIImage的可見繪制區(qū)域 */
CGRect rect = (CGRect){0.f,0.f,size};
/* 創(chuàng)建基于位圖的上下文 */
UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
/* 在當前位圖上下文添加圓角繪制路徑 */
CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
/* 當前繪制路徑和原繪制路徑相交得到最終裁剪繪制路徑 */
CGContextClip(UIGraphicsGetCurrentContext());
/* 繪制 */
[self drawInRect:rect];
/* 取得裁剪后的image */
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
/* 關閉當前位圖上下文 */
UIGraphicsEndImageContext();
return image;
}
- 美工切圖(可以讓美工切一張圓角的遮罩圖,蓋上去,也是很不錯的)
二、UILabel設置圓角
請參考的我另一篇文章有說明,鏈接奉上:http://www.itdecent.cn/p/08ef6e3f922b