设置UIView圆角 cornerRadius 圆角有性能问题,用贝塞尔曲线代替

81 篇文章 0 订阅
70 篇文章 0 订阅
[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. @interface UIView (RectCorner)  
  2.   
  3. @end  
  4.   
  5. @implementation UIView (RectCorner)  
  6. - (void)setCornerOnTop {  
  7.     UIBezierPath *maskPath;  
  8.     maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds  
  9.                                      byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)  
  10.                                            cornerRadii:CGSizeMake(10.0f10.0f)];  
  11.     CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];  
  12.     maskLayer.frame = self.bounds;  
  13.     maskLayer.path = maskPath.CGPath;  
  14.     self.layer.mask = maskLayer;  
  15.     [maskLayer release];  
  16. }  
  17. - (void)setCornerOnBottom {  
  18.     UIBezierPath *maskPath;  
  19.     maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds  
  20.                                      byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)  
  21.                                            cornerRadii:CGSizeMake(10.0f10.0f)];  
  22.     CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];  
  23.     maskLayer.frame = self.bounds;  
  24.     maskLayer.path = maskPath.CGPath;  
  25.     self.layer.mask = maskLayer;  
  26.     [maskLayer release];  
  27. }  
  28. - (void)setAllCorner {  
  29.     UIBezierPath *maskPath;  
  30.     maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds  
  31.                                           cornerRadius:10.0];  
  32.     CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];  
  33.     maskLayer.frame = self.bounds;  
  34.     maskLayer.path = maskPath.CGPath;  
  35.     self.layer.mask = maskLayer;  
  36.     [maskLayer release];  
  37. }  
  38. - (void)setNoneCorner{  
  39.     self.layer.mask = nil;  
  40. }  
  41.   
  42. @end  


-----------------------------
2,这是给UIImageView的image添加圆角的方法(如果是加载网络图片必须等图片加载完成后调用):

- (UIImageView *)imageWithRoundedCornersSize:(float)cornerRadius 
{
    UIImageView *imageView = self;
    UIImage * original = self.image;
    // Begin a new image that will be the new image with the rounded corners
    // (here with the size of an UIImageView)
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale); //[UIScreen mainScreen].scale
    
    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
                                cornerRadius:cornerRadius] addClip];
    // Draw your image
    [original drawInRect:imageView.bounds];
    
    // Get the image, here setting the UIImageView image
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();
    
    return imageView;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值