//
// UIImage+YKD.h
#import <UIKit/UIKit.h>
@interface UIImage (YKD)
/**
* 裁剪图片为圆形加边框
*
* @param image 需要被裁剪的图片
* @param borderWidth 边框大小
* @param color 边框颜色
*/
+ (UIImage *)imageWithClipImage:(UIImage *)image borderWidth:(CGFloat)border borderColor:(UIColor *)color;
@end
//
// UIImage+YKD.m
#import "UIImage+YKD.h"
@implementation UIImage (YKD)
+ (UIImage *)imageWithClipImage:(UIImage *)image borderWidth:(CGFloat)border borderColor:(UIColor *)color
{
// 设置图片的宽度和高度
CGFloat imageWH = image.size.width;
// 设置圆环的宽度和高度
CGFloat ovalWH = imageWH + border * 2;
// 1.开启一个位图上下文(跟图片尺寸一样大)
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
// 2.设置圆形裁剪区域,正切图片
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalWH, ovalWH)];
[color set];
[path fill];
// 3.设置裁剪区域
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH, imageWH)];
[clipPath addClip];
// 4.绘制图片
[image drawAtPoint:CGPointMake(border, border)];
// 5.获取裁剪后的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 6.关闭上下文
UIGraphicsEndImageContext();
return newImage;
}
@end