使View变圆的方法

 1.第一种方法:
         直接传入视图和直径即可
    /***这种方法只能画出圆***/
     -(
void)setRoundedView:(UIImageView *)roundedView toDiameter:(float)newSize
    {
     CGPoint saveCenter = roundedView.center;
     CGRect newFrame =CGRectMake(roundedView.frame.origin.x,   roundedView.frame.origin.y, newSize, newSize);
     roundedView.frame = newFrame;
     roundedView.layer.cornerRadius = newSize / 
2.0;   //半径
     roundedView.center = saveCenter;
    }

    2.
第二种方法:
    CAShapeLayer
把图片做成圆形效果
    
    
利用CAShapeLayer可以制作出任意的几何图形,把它作为UIImageView的遮罩,达到把图片做成圆形效果。
    
//创建个人主页头部的用户头像
    
self.userHead = [[UIImageView alloc]initWithFrame:CGRectMake(10358080)];
    
self.userHead.image = [UIImage imageNamed:@"start.jpg"];
    
    
//创建圆形遮罩,把用户头像变成圆形
        /*****顺时针画弧形,最终也可以画成圆形****/
    UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(4040radius:40 startAngle:0 endAngle:2*M_PI clockwise:YES];
    
CAShapeLayer* shape = [CAShapeLayer layer];
    shape.
path = path.CGPath;
    
self.userHead.layer.mask = shape;
    [self addSubview:self.userHead];

   3.第三种方法:
    self.littleImgView = [[UIImageView allocinitWithFrame:CGRectMake(self.frame.size.width-15, -52020)];
    
self.littleImgView.backgroundColor = [UIColor redColor];
    
self.littleImgView.hidden = YES;
    
self.littleImgView.layer.masksToBounds = YES;
    
self.littleImgView.layer.cornerRadius = 10;
    
    [self addSubview:_littleImgView];


       4.第四种方法
       //这里是椭圆操作
    //能过代码对画布裁剪成圆形》然后再将原始图像画出来
    -(
UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset 
{
    UIGraphicsBeginImageContext(image.size);
     /* 1.拿到画布 */
    
CGContextRef context = UIGraphicsGetCurrentContext();
     /* 2.设置绘画的属性 */
     //设置线条的宽度
    
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
    [image 
drawInRect:rect];
    
CGContextAddEllipseInRect(context, rect);
    
CGContextStrokePath(context);
    
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    
UIGraphicsEndImageContext();
    
return newimg;
}

   //上面代码注意 如果裁剪后没有使用 CGContextAddEllipseInRect(context, rect);CGContextStrokePath(context); 这条代码 就会引起背景为白色时看不出来任务效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值