iOS剪切图片的三种方法

第一种方法:通过设置layer的属性

最简单的一种,但是很影响性能,一般在正常的开发中使用很少.

lable.clipsToBounds = YES;(耗内存)
lable.layer.cornerRadius = 50;

第二种方法:通过UIGraphics和贝塞尔曲线进行绘制

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 400, 100, 100)];
    imageView.image = [UIImage imageNamed:@"ask"];
    imageView.image = [self imageWithCornerRadius:50 withImageView:imageView];
    [self.view addSubview:imageView];

- (UIImage *)imageWithCornerRadius:(CGFloat)radius withImageView:(UIImageView *)imageView{
    CGRect rect = (CGRect){0.f, 0.f, imageView.frame.size};
    UIGraphicsBeginImageContextWithOptions(imageView.frame.size, NO, UIScreen.mainScreen.scale);
    CGContextAddPath(UIGraphicsGetCurrentContext(),[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
    CGContextClip(UIGraphicsGetCurrentContext());
    
    [imageView.image drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
   
}

第三种方法:使用CAShapeLayer和UIBezierPath设置圆角

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image = [UIImage imageNamed:@"1"];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
    maskLayer.frame = imageView.bounds;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    imageView.layer.mask = maskLayer;
    [self.view addSubview:imageView];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前记 其实想写这个关于无限轮播的记录已经很久很久了,只是没什么时间,这只是一个借口,正如:时间就像海绵,挤一挤还是...
    半笑半醉間阅读 1,162评论 0 5
  • 第一种:系统自带压缩方式 用法:NSData *dataImage = UIImageJPEGRepresent...
    IOSMan阅读 13,741评论 0 6
  • 文/顾与归 夏日的阳光舞着 你似一束耀眼的火光 肆意溅落 我不敢前往,怕惊扰了你 又不舍挪脚离开,盼你回头 畅想着...
    顾与归阅读 249评论 2 0
  • 很多微友调侃世界上最长的路就是 --套路、其实不管什么路、不管带什么套那都是成长之路,只要我们提起觉性,时刻看到而...
    大智LSQ阅读 277评论 1 2
  • 今天的阳光有点毒辣!就像一个男人无知而傲慢的样子有一点点刺人。并不是甘愿委屈了自己!并不是在对谁摇尾乞怜!但谁...
    清泉水阅读 224评论 0 0