首页 > 其他 > 详细

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

时间:2015-07-07 09:35:35      阅读:952      评论:0      收藏:0      [点我收藏+]
  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;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

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

原文:http://blog.csdn.net/shaobo8910/article/details/46779259

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!