您的当前位置:首页生成不消耗内存的圆角图片

生成不消耗内存的圆角图片

2024-12-15 来源:哗拓教育

#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
显示全文