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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// CameraView.m
// Lighting
//
// Created by mac on 16/5/24.
// Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//
#import "CameraView.h"
#import "NSObject+UIImagePickerController.h"
#import "CustomUIImagePickerController.h"
@interface CameraView ()
{
UIImagePickerController *_imagePickerController;
}
@end
@implementation CameraView
- (void)viewDidLoad {
[super viewDidLoad];
_imagePickerController = [[CustomUIImagePickerController alloc] init];
_imagePickerController.delegate = self;
_imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
_imagePickerController.allowsEditing = YES;
[self initView];
// Do any additional setup after loading the view.
}
-(void)initView
{
self.subView=[[UIView alloc]init];
self.subView.bounds=CGRectMake(0, 0, 300, 150);
self.subView.center=self.view.center;
self.subView.layer.masksToBounds = YES;
self.subView.layer.cornerRadius = 10;
self.subView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:self.subView];
UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
[btn1 setTitle:@"拍照" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
btn1.titleLabel.textAlignment=NSTextAlignmentCenter;
btn1.tag=101;
[btn1 addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.subView addSubview:btn1];
UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake(0, 50, 300, 50)];
[btn2 setTitle:@"从相册中选取" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
btn2.titleLabel.textAlignment=NSTextAlignmentCenter;
btn2.tag=102;
[btn2 addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.subView addSubview:btn2];
UIButton *btn3=[[UIButton alloc]initWithFrame:CGRectMake(0, 100, 300, 50)];
[btn3 setTitle:@"取消" forState:UIControlStateNormal];
[btn3 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
btn3.titleLabel.textAlignment=NSTextAlignmentCenter;
btn3.tag=103;
[btn3 addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.subView addSubview:btn3];
//点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(DismissScreenView:)];
tap.delegate = self;
tap.cancelsTouchesInView = NO;
[[UIApplication sharedApplication].keyWindow addGestureRecognizer:tap];
}
-(void)selectedBtn:(UIButton*)sender
{
switch (sender.tag) {
case 101://拍照
{
[self selectImageFromCamera];
}
break;
case 102://从相册中选取
{
[self selectImageFromAlbum];
}
break;
case 103://取消
{
[self DismissScreenView];
}
break;
default:
break;
}
}
#pragma mark 从摄像头获取图片或视频
- (void)selectImageFromCamera
{
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//在打开相机之前,设置屏幕为支持竖屏
[DeviceDirectionManager instance].isHorizontal=YES;
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
#pragma mark 从相册获取图片
- (void)selectImageFromAlbum
{
//NSLog(@"相册");
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//在打开相册之前,设置屏幕为支持竖屏
[DeviceDirectionManager instance].isHorizontal=YES;
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
//适用获取所有媒体资源,只需判断资源类型
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
//判断资源类型
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
//如果是图片
self.customImage= info[UIImagePickerControllerEditedImage];
if ([self.delegate respondsToSelector:@selector(addCustomPictureImage:)]) {
[self.delegate addCustomPictureImage:self.customImage];
}
}else{
}
[self dismissViewControllerAnimated:YES completion:nil];
[DeviceDirectionManager instance].isHorizontal=NO;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
[DeviceDirectionManager instance].isHorizontal=NO;
}
#pragma mark 图片保存完毕的回调
- (void) image: (UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo: (void *)contextInf{
}
#pragma mark 视频保存完毕的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf{
if (error) {
NSLog(@"保存视频过程中发生错误,错误信息:%@",error.localizedDescription);
}else{
NSLog(@"视频保存成功.");
}
}
//取消按钮
-(void)DismissScreenView
{
if (self) {
if ([self.delegate respondsToSelector:@selector(restCamerBtnImage)]) {
[self.delegate restCamerBtnImage];
}
[self.view removeFromSuperview];
}
}
//取消当前页面点击手势
-(void)DismissScreenView:(UITapGestureRecognizer*)sender{
CGPoint point = [sender locationInView:self.view];
if (point.x<self.subView.frame.origin.x || point.x >self.subView.frame.origin.x+self.subView.frame.size.width||point.y<self.subView.frame.origin.y||point.y>self.subView.frame.origin.y+self.subView.frame.size.height) {
if (self) {
if ([self.delegate respondsToSelector:@selector(restCamerBtnImage)]) {
[self.delegate restCamerBtnImage];
}
[self.view removeFromSuperview];
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end