UIImageView设置圆形图片的三种方法

//创建UIImageView
UIImageView *imageV = [UIImageView alloc] initWithFrame:CGMake(0,0,100,100)];

[view addSubView:imageV];

如果需要处理的图形比较多,最好利用第二种方法里面的2个方法去实现

1.第一种方法:设置UIImageView的layer的圆角,然后裁剪 (这种方法比较损耗性能)
imageV.layer.cornerRadius = imageV.Frame.size.Width;
imageV.layer.masksToBounds = YES;

2.利用贝塞尔将imageV整个类容画在一个图形上下文中,在图形上下文中画成圆形

给UIImageView空充一个分类,分别实现2个方法

  • (void)drawRoundImageToImageView
    {
    //设置图片上下文的大小
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0);

    [[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.bounds.size.width]addClip];

    [self drawRect:self.bounds];

    self.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    }

  • (void)drawShapeLayerRoundRect
    {
    UIBezierPath *maskPaht = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.bounds.size];

    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];

    shapeLayer.frame = self.bounds;

    shapeLayer.path = maskPaht.CGPath;

    self.layer.mask = shapeLayer;
    }

你可能感兴趣的:(UIImageView设置圆形图片的三种方法)