美文网首页
生成不消耗内存的圆角图片

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

作者: iOS_Cqlee | 来源:发表于2016-03-07 21:34 被阅读315次
    
    #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
    

    相关文章

      网友评论

          本文标题:生成不消耗内存的圆角图片

          本文链接:https://www.haomeiwen.com/subject/nsfzkttx.html