#import <UIKit/UIKit.h>
@interface UIImage (CqCircleImageExtension)
- (instancetype)cq_circleImage;
+ (instancetype)cq_circleImage:(NSString *)name;
@end
----------------------------------------------------
#import "UIImage+CqCircleImageExtension.h"
@implementation UIImage (CqCircleImageExtension)
- (instancetype)cq_circleImage{
//开启上下文
UIGraphicsBeginImageContext(self.size);
//上下文
CGContextRef ref = UIGraphicsGetCurrentContext();
//添加一个圈
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextAddEllipseInRect(ref, rect);
//裁剪
CGContextClip(ref);
//画在上下文
[self drawInRect:rect];
//获得新的图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (instancetype)cq_circleImage:(NSString *)name{
return [[self imageNamed:name] cq_circleImage];
}
@end