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( 10 , 35 , 80 , 80 )];
self .userHead.image = [UIImage imageNamed: @"start.jpg" ];
// 创建圆形遮罩,把用户头像变成圆形
/*****顺时针画弧形,最终也可以画成圆形****/
UIBezierPath
* path = [
UIBezierPath
bezierPathWithArcCenter
:
CGPointMake
(
40
,
40
)
radius
:
40
startAngle
:
0
endAngle
:
2
*
M_PI
clockwise
:
YES
];
CAShapeLayer * shape = [ CAShapeLayer layer ];
shape. path = path. CGPath ;
self .userHead.layer.mask = shape;
self.littleImgView.backgroundColor = [UIColor redColor];
self.littleImgView.hidden = YES;
self.littleImgView.layer.masksToBounds = YES;
self.littleImgView.layer.cornerRadius = 10;
-(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset
CAShapeLayer * shape = [ CAShapeLayer layer ];
shape. path = path. CGPath ;
self .userHead.layer.mask = shape;
[self addSubview:self.userHead];
3.第三种方法:
self.littleImgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width-15, -5, 20, 20)];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();
CGContextRef context = UIGraphicsGetCurrentContext();
/* 2.设置绘画的属性 */
//设置线条的宽度
CGContextSetLineWidth(context, 2);
CGContextSetLineWidth(context, 2);
//设置颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//设置区域
CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);
CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);
//画椭圆
CGContextAddEllipseInRect(context, rect);
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); 这条代码 就会引起背景为白色时看不出来任务效果。
CGContextClip(context);
//绘制image
[image drawInRect:rect];
CGContextAddEllipseInRect(context, rect);
CGContextStrokePath(context);
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimg;
}
//上面代码注意 如果裁剪后没有使用 CGContextAddEllipseInRect(context, rect);CGContextStrokePath(context); 这条代码 就会引起背景为白色时看不出来任务效果。