// 1. 算出这个 imageview 的大小
CGRect rect = CGRectMake(0, 0, size.width, size.height);
// 2. 创建不带 alpha 通道的上下文
UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
CGContextRef ref = UIGraphicsGetCurrentContext();
// 3. 把图片画到这个这个画布上
// 3.1 绘制圆形区域(此处根据宽度来设置)
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
CGContextAddPath(UIGraphicsGetCurrentContext(), path.CGPath);
// 3.2 裁剪绘图区域
CGContextClip(ref);
// 3.3 绘制图片
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 4. 从画布上把图取出来
UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
// 5. 关闭图形上下文
UIGraphicsEndImageContext();
return output;
近期评论