如何创建圆形头像和圆角图片
//以前没坚持写 后悔莫及
希望可以结识更多的朋友,写的不好请多多包涵,请多多指教 谢谢
今天说一下IOS如何将图片进行圆角化和如何很快的就做一个圆形头像出来。
IOS给我们提供了库函数<QuartzCore/QuartzCore.h>,我们可以根据这个库中很多方法来快速做出一个圆形头型,实现一个图片的圆角化。
先来展示一下效果(为区别,背景采用了黑色):
图2 为将原图直接‘剪切’成为圆形
图3 为在图2基础上加上边框
图4 为将原图’剪切‘为圆角图片
图5 为在图4的基础上加上边框
接下来奉上代码:
//圆形图像变化 如图二 //将方形图片变成圆形 需要将半径设为 原图像宽度的一半 self.imageView2.layer.cornerRadius = self.imageView2.frame.size.width / 2; //将剪切去的一部分隐藏起来 self.imageView2.clipsToBounds = YES;
//圆形图像变化并加上边框 如图三 //将方形图片变成圆形 需要将半径设为 原图像宽度的一半 self.imageView3.layer.cornerRadius = self.imageView2.frame.size.width / 2; //将剪切去得一部分隐藏起来 self.imageView3.clipsToBounds = YES; //添加边框 //设置边框的大小 self.imageView3.layer.borderWidth = 3.0f; //设置边框的颜色 self.imageView3.layer.borderColor = [UIColor redColor].CGColor;
//将图像变成圆角图片 如图四 self.imageView4.layer.cornerRadius = 10; //遮掩剪切部分 self.imageView4.layer.masksToBounds = YES;
//将图片变成圆角图片变成带边框 如图五 self.imageView5.layer.cornerRadius = 10; self.imageView5.layer.masksToBounds = YES; self.imageView5.layer.borderWidth = 3.0f; self.imageView5.layer.borderColor =[UIColor redColor].CGColor;
欢迎各位批评指正