//
// ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"阿狸头像"];
CGFloat imageWH = image.size.width;
// 设置圆环的宽度
CGFloat border = 1;
// 设置圆环的宽度和高度
CGFloat ovalWH = imageWH + border * 2;
// 1.开启一个位图上下文(跟图片尺寸一样大)
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
// 2.设置圆形裁剪区域,正切图片
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalWH, ovalWH)];
[[UIColor redColor] set];
[path fill];
// 3.设置裁剪区域
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH, imageWH)];
[clipPath addClip];
// 4.绘制图片
[image drawAtPoint:CGPointMake(border, border)];
// 5.获取裁剪后的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 6.关闭上下文
UIGraphicsEndImageContext();
// 7.把裁剪后的图片显示出来
self.imageView.image = newImage;
}
- (void)clipImage
{
UIImage *image = [UIImage imageNamed:@"阿狸头像"];
// 1.开启一个位图上下文(跟图片尺寸一样大)
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
// 2.设置圆形裁剪区域,正切图片
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[path addClip];
// 3.绘制图片
[image drawAtPoint:CGPointZero];
// 4.从上下文中获取图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 5.关闭上下文
UIGraphicsEndImageContext();
// 6.把裁剪后的图片显示出来
self.imageView.image = newImage;
}
@end
Quartz2D - 图片裁剪为正圆形2(加边框)
最新推荐文章于 2019-04-17 23:03:19 发布