1.首先看下效果图吧
2.今天在实现这个功能时,直接将这个功能封装起来了,大家可以将代码直接拿过去使用;
#import <UIKit/UIKit.h>
@interface UIImage (image)
-(UIImage *)newImageWithBoradColor:(UIColor *)color boradWidth:(CGFloat)width image:(UIImage *)image;
@end
#import "UIImage+image.h"
@implementation UIImage (image)
//传入边框的颜色和边框的尺寸,以及你想裁剪的图片,生成一张裁剪好的图片
-(UIImage *)newImageWithBoradColor:(UIColor *)color boradWidth:(CGFloat)width image:(UIImage *)image{
//开启图片上下文
UIGraphicsBeginImageContextWithOptions(image.size,NO, 0.0);
//绘制外面边框的路径
UIBezierPath *largePath = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0, image.size.width, image.size.height)];
//设置内容颜色
[color set];
//填充边框内部
[largePath fill];
//绘制图片路径
UIBezierPath *smallPath = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(width, width, image.size.width -2 * width, image.size.height -2 * width)];
//剪裁路径
[smallPath addClip];
//渲染到上下文
[image drawAtPoint:CGPointZero];
//根据上面上下文,生成一张新的图片
UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
//返回新生成的图片
return image = newImage;
}
@end