ios實現圖片圓角方式小計

weixin_33782386發表於2017-07-25
  1. 最簡單的方式就是要美工妹子給你給你切一張帶圓角的圖片,用iimageNamed:@""載入圖片。
  2. 通過圖層layer實現,不過如果是tableView上面多處用到了圖片圓角 使用layer會造成離屏渲染,會加大GPU的開銷,在效能上會大大折扣,不建議使用。
self.imageView.layer.masksToBounds = yes;
self.imageView.layer.cornerRadius = 10;
  1. 通過QuartzCore實現:
-(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {
        UIGraphicsBeginImageContext(image.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, 2);
        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
        CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f,   image.size.height - inset * 2.0f);
        CGContextAddEllipseInRect(context, rect);
        CGContextClip(context);
        [image drawInRect:rect];
        CGContextAddEllipseInRect(context, rect);
        CGContextStrokePath(context);
        UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newimg;
    }
  1. 利用貝塞爾曲線畫出來
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    UIImage *image =[UIImage imageNamed:@"headerImage"];
    UIGraphicsBeginImageContextWithOptions(self.imgView.bounds.size, NO, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:self.imgView.bounds cornerRadius:self.imgView.bounds.size.width/2.0]addClip];
    [image drawInRect:self.imgView.bounds];
    self.imgView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

相關文章