UIView *view = [[UIView alloc]init];
view.frame = CGRectMake(50, 50, 50,50);
view.backgroundColor = [UIColor redColor];
// 圓角
view.layer.masksToBounds = YES;
view.layer.cornerRadius = 0;
view.layer.borderWidth = 0.5;
view.layer.borderColor = [[UIColor blackColor] CGColor];
[self.view addSubview:view];
UIImageView添加圓角
- 最直接的方法就是使用如下屬性設置:
imgView.layer.cornerRadius = 10; // 這一行代碼是很消耗性能的 imgView.clipsToBounds = YES;
**這是離屏渲染(off-screen-rendering),消耗性能的**
-
給UIImage添加生成圓角圖片的擴展API:這是on-screen-rendering
- (UIImage *)imageWithCornerRadius:(CGFloat)radius { CGRect rect = (CGRect){0.f, 0.f, self.size}; UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale); CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath); CGContextClip(UIGraphicsGetCurrentContext()); [self drawInRect:rect]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
原文鏈接:http://www.jianshu.com/p/5d2163640e26
著作權歸作者所有,轉載請聯繫作者獲得授權,並標註“簡書作者”。