美文网首页
图片圆角

图片圆角

作者: 下雨之後 | 来源:发表于2016-11-04 16:09 被阅读13次
    UIImage+XMGExtension

    .h文件

    #import <UIKit/UIKit.h>
    
    @interface UIImage (XMGExtension)
    /**
     * 圆形图片
     */
    - (UIImage *)circleImage;
    @end
    

    .m文件

    #import "UIImage+XMGExtension.h"
    
    @implementation UIImage (XMGExtension)
    - (UIImage *)circleImage
    {
        // NO代表透明
        UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
        
        // 获得上下文
        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;
    }
    @end
    
    UIImageView+XMGExtension

    .h文件

    #import <UIKit/UIKit.h>
    
    @interface UIImageView (XMGExtension)
    - (void)setCircleImageWithUrl:(NSString *)url;
    @end
    

    .m文件

    #import "UIImageView+XMGExtension.h"
    #import <UIImageView+WebCache.h>
    #import "UIImage+XMGExtension.h"
    
    @implementation UIImageView (XMGExtension)
    - (void)setCircleImageWithUrl:(NSString *)url
    {
        UIImage *placeholder = [[UIImage imageNamed:@"defaultUserIcon"] circleImage];
        [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.image = image ? [image circleImage] : placeholder;
        }];
    }
    @end
    

    说明:出自MJ

    相关文章

      网友评论

          本文标题:图片圆角

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