iOS图片处理之切圆角

方法一:
自己画出圆角的路径,剪切!

+ (UIImage *)rh_getCornerImage:(UIImage *)image
                  cornerRadius:(CGFloat)cornerRadius {
    UIImage *cornerImage = image;
    
    CGFloat width = image.size.width * image.scale;
    CGFloat height = image.size.height * image.scale;
    CGFloat radius = cornerRadius * image.scale;
    CGSize size = CGSizeMake(width, height);
    
    CGRect rect = (CGRect){CGPointZero, size};
    UIGraphicsBeginImageContextWithOptions(size, false, 1.0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 0, radius);
    CGContextAddArcToPoint(context, 0, 0, radius, 0, radius);
    CGContextAddLineToPoint(context, width - radius, 0);
    CGContextAddArcToPoint(context, width, 0, width, radius, radius);
    CGContextAddLineToPoint(context, width, height - radius);
    CGContextAddArcToPoint(context, width, height, width - radius, height, radius);
    CGContextAddLineToPoint(context, radius, height);
    CGContextAddArcToPoint(context, 0, height, 0, height - radius, radius);
    CGContextAddLineToPoint(context, 0, radius);
    CGContextClosePath(context);
    
    CGContextClip(context);
    
    [image drawInRect:rect];       // 画图
    CGContextDrawPath(context, kCGPathFill);
    // CGContextDrawImage(context, rect, cornerImage.CGImage); // 同上
    
    cornerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return cornerImage;
}

方案二:
UIBezierPath裁剪

// UIBezierPath 裁剪
+ (UIImage *)rh_bezierPathClip:(UIImage *)img
                  cornerRadius:(CGFloat)cornerRadius {
    int w = img.size.width * img.scale;
    int h = img.size.height * img.scale;
    CGRect rect = (CGRect){CGPointZero, CGSizeMake(w, h)};

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(w, h), false, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius] addClip];
    [img drawInRect:rect];
    UIImage *cornerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return cornerImage;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容