图片圆角

UIImage+XMGExtension

.h文件

#import 

@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 

@interface UIImageView (XMGExtension)
- (void)setCircleImageWithUrl:(NSString *)url;
@end

.m文件

#import "UIImageView+XMGExtension.h"
#import 
#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

你可能感兴趣的:(图片圆角)