IOS开发之——画板-照片(4)

一 概述

  • 从相册选择照片
  • 将照片传递给HandleImageView,并处理图片的手势操作(捏合,缩放等)
  • 手势处理后,长按图片,截屏后,可对图片进行操作

二 功能实现

2.1 HandleImageView-接收图片,并进行手势识别处理

HandleImageView.h

1
2
3
4
5
6
7
8
#import <UIKit/UIKit.h>
typedef void(^HandleImageViewBlock) (UIImage *image);

@interface HandleImageView : UIView
@property (nonatomic,strong) UIImage *image;
@property (nonatomic,copy) HandleImageViewBlock block;

@end

HandleImageView.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#import "HandleImageView.h"
#import "UIImage+Tool.h"

@interface HandleImageView()<UIGestureRecognizerDelegate>
@property (nonatomic,weak) UIImageView *imageView;
@end

@implementation HandleImageView

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//添加UIImageView
[self addImageView];
//添加手势
[self addGestureRecognizers];
}
return self;
}
-(void)addGestureRecognizers
{
//1.长按
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[_imageView addGestureRecognizer:longPress];
[self addPinch];
[self addRotation];
}
//长按方法
-(void)longPress:(UILongPressGestureRecognizer *)longPress
{
if(longPress.state==UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:0.5 animations:^{
_imageView.alpha=0.3;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
_imageView.alpha=1;
} completion:^(BOOL finished) {
//1.截屏
UIImage *newImage=[UIImage imageWithCaptureView:self];
//2.把图片传给控制器
_block(newImage);
//3.把自己移除父控制器
[self removeFromSuperview];
}];
}];
}
}
#pragma mark-捏合
-(void)addPinch
{
UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
// 设置代理的原因:想要同时支持多个手势
pinch.delegate = self;
[_imageView addGestureRecognizer:pinch];
}
-(void)pinch:(UIPinchGestureRecognizer *)pinch
{
_imageView.transform=CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
//复位
pinch.scale=1;
}
// Simultaneous:同时
// 默认是不支持多个手势
// 当你使用一个手势的时候就会调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (void)addRotation
{
// rotation
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[_imageView addGestureRecognizer:rotation];
}

- (void)rotation:(UIRotationGestureRecognizer *)rotation
{

// _imagView.transform = CGAffineTransformMakeRotation(rotation.rotation);
_imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);

// 复位
rotation.rotation = 0;
}
-(void)setImage:(UIImage *)image
{
_image=image;
_imageView.image=image;
}

-(void)addImageView
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.bounds];
imageView.userInteractionEnabled=YES;
_imageView=imageView;
[self addSubview:imageView];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
@end

2.2 PaintView.m-将HandleImageView后的图片传递给ViewController

1
2
3
4
5
6
-(void)setImage:(UIImage *)image
{
_image=image;
[self.paths addObject:image];
[self setNeedsDisplay];
}

2.3 ViewController.m-选择照片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//选择照片
- (IBAction)selectPicture:(UIBarButtonItem *)sender
{
//去用户的相册
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
//数据源
picker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.delegate=self;
[self presentViewController:picker animated:YES completion:nil];

}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info
{
NSLog(@"%@",info);
UIImage *image=info[UIImagePickerControllerOriginalImage];
HandleImageView *handImageView=[[HandleImageView alloc]initWithFrame:self.paintView.frame];
handImageView.block = ^(UIImage * _Nonnull image) {
_paintView.image=image;
};
handImageView.image=image;
[self.view addSubview:handImageView];
[self dismissViewControllerAnimated:YES completion:nil];
}

2.4 效果图

说明:按住Option按键,屏幕上出现2个触摸点时,可进行手势操作