图片圆角

108 阅读1分钟
#import <UIKit/UIKit.h>

@interface UIImage (DCCircle)

/**
 *  返回圆形图片
 */
- (instancetype)dc_circleImage;

+ (instancetype)dc_circleImage:(NSString *)name;

@end
#import "UIImage+DCCircle.h"

@implementation UIImage (DCCircle)

- (instancetype)dc_circleImage {
    
    // 开启图形上下文
    UIGraphicsBeginImageContext(self.size);
    // 上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 添加一个圆
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextAddEllipseInRect(ctx, rect);
    
    // 裁剪
    CGContextClip(ctx);
    // 绘制图片
    [self drawInRect:rect];
    // 获得图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭图形上下文
    UIGraphicsEndImageContext();
    
    return image;
}

+ (instancetype)dc_circleImage:(NSString *)name {
    
    return [[self imageNamed:name] dc_circleImage];
}



@end