//
// 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 发布
本文介绍了一种在iOS应用中实现图片圆角裁剪的方法。通过使用UIKit框架中的UIGraphicsBeginImageContextWithOptions等API创建位图上下文,并利用UIBezierPath定义圆形路径进行裁剪,最终实现了将指定图片裁剪为圆角效果的功能。
3125

被折叠的 条评论
为什么被折叠?



