裁剪圆形头像

circlePhoto1.png

裁剪圆形头像###

//裁剪圆形头像
-(void)clipRoundPhoto{
    //打开图片上下文
    UIImage *image = [UIImage imageNamed:@"阿狸头像"];
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 1);
    
    //设置裁剪区域
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    [path addClip];
    
    //画图像
    [image drawAtPoint:CGPointZero];
    
    //导出图片
    UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext();
    
    //显示到imageView
    self.imageView.image = newimage;
}
circlePhoto2.png

裁剪带边框的圆形头像###

ViewController.m文件

#import "UIImage+image.h"

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageView.image = [UIImage imageWithBorder:15 borderColor:[UIColor colorWithRed:1.0 green:0.8 blue:0.0 alpha:1.0] image:[UIImage imageNamed:@"阿狸头像"]];
}

UIImage+image.m 分类文件

+(instancetype)imageWithBorder:(CGFloat)borderWidth borderColor:(UIColor *)borderColor image:(UIImage *)image{
    //开启图片上下文
    CGSize size = CGSizeMake(image.size.width+borderWidth*2, image.size.height+borderWidth*2);
    UIGraphicsBeginImageContextWithOptions(size, NO, 1);
    
    //大圆背景
    UIBezierPath * bigCirclePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
    [borderColor set];
    [bigCirclePath fill];
    
    //设置裁剪小圆路径
    UIBezierPath * smallCirclePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderWidth, borderWidth, image.size.width, image.size.height)];
    [smallCirclePath addClip];
    
    //画图片
    [image drawAtPoint:CGPointMake(borderWidth, borderWidth)];
    
    //导出图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图片上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,031评论 3 119
  • 观察水仙(四) 水仙开花了 水仙开花了!这个消息我星期一就知道了。我们组的水仙开的花非常美,感觉非常非常...
    小太阳班令伊禾阅读 231评论 0 0
  • 前天午休班内220两个男生吵嘴被通报,我还没来得及质问,寝室长森就找到我,给我说午休发生的事情经过。 大致就是他不...
    玉瑾阅读 245评论 0 1
  • 有很多的时候,都会觉得无聊,都会有一种想要去做一些对自己有用的事情的冲动,可是呢,到最后发现自己不知道该要做什么...
    灵绎阅读 321评论 0 0
  • “二月八”是啥?犁耙会呀! 它是一场物资交流会?传统展示会?农民狂欢会?去了你就知道啦! 犁耙会的来历: 据说保山...
    笨学徒阅读 857评论 6 5