image 切圆形头像

为 UIimage 添加 类别

-(UIImage *)circleImage
{

// 2.开启上下文
CGFloat imageW = self.size.width;
CGFloat imageH = self.size.height;
CGSize imageSize = CGSizeMake(imageW, imageH);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);

// 3.取得当前的上下文,这里得到的就是上面刚创建的那个图片上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

// 4.画边框(大圆)

CGFloat bigRadius = imageW * 0.5; // 大圆半径
CGFloat centerX = bigRadius; // 圆心
CGFloat centerY = bigRadius;
CGContextAddArc(ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0);
CGContextFillPath(ctx); // 画圆。As a side effect when you call this function, Quartz clears the current path.

// 5.小圆
CGFloat smallRadius = bigRadius;
CGContextAddArc(ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0);
// 裁剪(后面画的东西才会受裁剪的影响)
CGContextClip(ctx);

// 6.画图
[self drawInRect:CGRectMake(0,0, self.size.width, self.size.height)];

// 7.取图
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

// 8.结束上下文
UIGraphicsEndImageContext();

return newImage;

}

UIImageView 的类别 拓展 添加下载方法 (采用sdWebImage 的 下载)

-(void)jk_setImageWithURl:(NSString *)url placeholderImage:(UIImage *)hoderImage{

NSURL * imageUrl = [NSURL URLWithString:url];

[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageUrl options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
    
    
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
   
    if (data == nil) {
        
        self.image = hoderImage;
    }
    
    self.image = [image circleImage];;
    
}];

}

你可能感兴趣的:(image 切圆形头像)