MyImageView
#import "MyImageView.h"
@implementation MyImageView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
/** uiimageview這個(gè)類不會(huì)自動(dòng)畫線 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. 獲取起始點(diǎn) 并且創(chuàng)建路徑
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:[[touches anyObject] locationInView:self]];
// 2. 裝入數(shù)組
[self.pathArray addObject:path];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 1. 獲取終點(diǎn) (不止一個(gè))
UIBezierPath *path = [self.pathArray lastObject];
[path addLineToPoint:[[touches anyObject] locationInView:self]];
// 2. 調(diào)用drawRect(drawrect不會(huì)自動(dòng)調(diào)用 需要使用以下方法)
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 封閉路徑
UIBezierPath *path = [self.pathArray lastObject];
[path closePath];
[self setNeedsDisplay];
//調(diào)用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]; //這種透明方式 不會(huì)讓子視圖也變透明(alpha是直接設(shè)置會(huì)讓子視圖也透明)
[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. 關(guān)閉上下文
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)];
//剪切個(gè)圓
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
// 3. 剪切 原來的圖不變
[path addClip];
//繪制
[image drawAtPoint:CGPointZero];
// 4 .需要接收剪切之后的圖
UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
// 5. 關(guān)閉上下文
UIGraphicsEndImageContext();
//顯示出來剪切的圖
myImageView.image = clipImage;
[self.view addSubview:myImageView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者