畫圓角

   UIView *view = [[UIView alloc]init];

    view.frame = CGRectMake(505050,50);

    view.backgroundColor = [UIColor redColor];

    // 圓角

    view.layer.masksToBounds = YES;

    view.layer.cornerRadius = 0;

    view.layer.borderWidth = 0.5;

    view.layer.borderColor = [[UIColor blackColorCGColor];

    

    [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;
    }


文/Jack_lin(簡書作者)
原文鏈接:http://www.jianshu.com/p/5d2163640e26
著作權歸作者所有,轉載請聯繫作者獲得授權,並標註“簡書作者”。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章