图片圆角

2016-11-04  本文已影响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

上一篇下一篇

猜你喜欢

热点阅读