MyImageView
#import "MyImageView.h"
@implementation MyImageView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
/** uiimageview这个类不会自动画线 image更改显示不出 要在控制器里写*/
//- (void)drawRect:(CGRect)rect {
// // Drawing code
// UIBezierPath *path = [UIBezierPath bezierPath];
// [path moveToPoint:CGPointMake(50, 50)];
// [path addLineToPoint:CGPointMake(100, 100)];
// path.lineWidth = 20;
// [path stroke];
//
//
//}
@end
MyView.h
#import <UIKit/UIKit.h>
typedef void (^PathBlock)(UIBezierPath *p);
@interface MyView : UIView
@property(nonatomic,copy)PathBlock pb;
@end
MyView.m
#import "MyView.h"
@interface MyView ()
@property(nonatomic,strong)NSMutableArray *pathArray;
@end
@implementation MyView
/** 懒加载pathArray */
- (NSMutableArray *)pathArray{
if (!_pathArray) {
_pathArray = [NSMutableArray array];
}
return _pathArray;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 1. 获取起始点 并且创建路径
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:[[touches anyObject] locationInView:self]];
// 2. 装入数组
[self.pathArray addObject:path];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 1. 获取终点 (不止一个)
UIBezierPath *path = [self.pathArray lastObject];
[path addLineToPoint:[[touches anyObject] locationInView:self]];
// 2. 调用drawRect(drawrect不会自动调用 需要使用以下方法)
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 封闭路径
UIBezierPath *path = [self.pathArray lastObject];
[path closePath];
[self setNeedsDisplay];
//调用block传递路径
self.pb(path);
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
for (UIBezierPath *path in self.pathArray) {
path.lineWidth = 2;
[path stroke];
}
}
@end
ViewController.m
#import "ViewController.h"
#import "MyImageView.h"
#import "MyView.h"
@interface ViewController ()
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)MyView *mv;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 获取图片
__block UIImage *image = [UIImage imageNamed:@"1.png"];
self.imageView = [[UIImageView alloc]initWithImage:image];
[self.view addSubview:self.imageView];
self.mv = [[MyView alloc]initWithFrame:self.imageView.frame];
self.mv.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0]; //这种透明方式 不会让子视图也变透明(alpha是直接设置会让子视图也透明)
[self.view addSubview:self.mv
];
//剪切
__weak typeof(self) weakSelf = self;
self.mv.pb = ^(UIBezierPath *p){
//获取上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//剪切路径
UIBezierPath *path = p;
//剪切
[path addClip];
//绘制
[image drawAtPoint:CGPointZero];
// 4 .需要接收剪切之后的图
image = UIGraphicsGetImageFromCurrentImageContext();
// 5. 关闭上下文
UIGraphicsEndImageContext();
//显示出来剪切的图
weakSelf.imageView.image = image;
};
}
/******** 剪切一张图 ************/
- (void)clipImage{
// 1. 拥有一张图
UIImage *image = [UIImage imageNamed:@"1.png"];
MyImageView *myImageView = [[MyImageView alloc]initWithImage:image];
CGRect rect = CGRectMake(50, 50, 0, 0);
rect.size = image.size;
myImageView.frame = rect;
//以上是剪切之前的图片显示
//获取上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
// 2. 需要剪裁的范围
// UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 50, 50)];
//剪切个圆
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
// 3. 剪切 原来的图不变
[path addClip];
//绘制
[image drawAtPoint:CGPointZero];
// 4 .需要接收剪切之后的图
UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
// 5. 关闭上下文
UIGraphicsEndImageContext();
//显示出来剪切的图
myImageView.image = clipImage;
[self.view addSubview:myImageView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end