/
PKImagePickerViewController.m
311 lines (263 loc) · 12.8 KB
/
PKImagePickerViewController.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// MyImagePickerViewController.m
// cameratestapp
//
// Created by pavan krishnamurthy on 6/24/14.
// Copyright (c) 2014 pavan krishnamurthy. All rights reserved.
//
#import "PKImagePickerViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface PKImagePickerViewController ()
@property(nonatomic,strong) AVCaptureSession *captureSession;
@property(nonatomic,strong) AVCaptureStillImageOutput *stillImageOutput;
@property(nonatomic,strong) AVCaptureDevice *captureDevice;
@property(nonatomic,strong) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
@property(nonatomic,assign) BOOL isCapturingImage;
@property(nonatomic,strong) UIImageView *capturedImageView;
@property(nonatomic,strong)UIImagePickerController *picker;
@property(nonatomic,strong) UIView *imageSelectedView;
@property(nonatomic,strong) UIImage *selectedImage;
@end
@implementation PKImagePickerViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)loadView
{
self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
}
-(BOOL)prefersStatusBarHidden
{
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.captureSession = [[AVCaptureSession alloc]init];
self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
self.capturedImageView = [[UIImageView alloc]init];
self.capturedImageView.frame = self.view.frame; // just to even it out
self.capturedImageView.backgroundColor = [UIColor clearColor];
self.capturedImageView.userInteractionEnabled = YES;
self.capturedImageView.contentMode = UIViewContentModeScaleAspectFill;
self.captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.captureSession];
self.captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.captureVideoPreviewLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.captureVideoPreviewLayer];
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
if (devices.count > 0) {
self.captureDevice = devices[0];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:self.captureDevice error:&error];
[self.captureSession addInput:input];
self.stillImageOutput = [[AVCaptureStillImageOutput alloc]init];
NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[self.stillImageOutput setOutputSettings:outputSettings];
[self.captureSession addOutput:self.stillImageOutput];
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
_captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
}
else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
_captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
}
UIButton *camerabutton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)/2-50, CGRectGetHeight(self.view.bounds)-100, 100, 100)];
[camerabutton setImage:[UIImage imageNamed:@"PKImageBundle.bundle/take-snap"] forState:UIControlStateNormal];
[camerabutton addTarget:self action:@selector(capturePhoto:) forControlEvents:UIControlEventTouchUpInside];
[camerabutton setTintColor:[UIColor blueColor]];
[camerabutton.layer setCornerRadius:20.0];
[self.view addSubview:camerabutton];
UIButton *flashbutton = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, 30, 31)];
[flashbutton setImage:[UIImage imageNamed:@"PKImageBundle.bundle/flash"] forState:UIControlStateNormal];
[flashbutton setImage:[UIImage imageNamed:@"PKImageBundle.bundle/flashselected"] forState:UIControlStateSelected];
[flashbutton addTarget:self action:@selector(flash:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:flashbutton];
UIButton *frontcamera = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)-50, 5, 47, 25)];
[frontcamera setImage:[UIImage imageNamed:@"PKImageBundle.bundle/front-camera"] forState:UIControlStateNormal];
[frontcamera addTarget:self action:@selector(showFrontCamera:) forControlEvents:UIControlEventTouchUpInside];
[frontcamera setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.2]];
[self.view addSubview:frontcamera];
}
if (!self.doNotNeedAlbum) {
UIButton *album = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)-35, CGRectGetHeight(self.view.frame)-40, 27, 27)];
[album setImage:[UIImage imageNamed:@"PKImageBundle.bundle/library"] forState:UIControlStateNormal];
[album addTarget:self action:@selector(showalbum:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:album];
}
UIButton *cancel = [[UIButton alloc]initWithFrame:CGRectMake(5, CGRectGetHeight(self.view.frame)-40, 32, 32)];
[cancel setImage:[UIImage imageNamed:@"PKImageBundle.bundle/cancel"] forState:UIControlStateNormal];
[cancel addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancel];
self.picker = [[UIImagePickerController alloc]init];
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.picker.delegate = self;
self.imageSelectedView = [[UIView alloc]initWithFrame:self.view.frame];
[self.imageSelectedView setBackgroundColor:[UIColor clearColor]];
[self.imageSelectedView addSubview:self.capturedImageView];
UIView *overlayView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-60, CGRectGetWidth(self.view.frame), 60)];
[overlayView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.9]];
[self.imageSelectedView addSubview:overlayView];
UIButton *selectPhotoButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetWidth(overlayView.frame)-40, 20, 32, 32)];
[selectPhotoButton setImage:[UIImage imageNamed:@"PKImageBundle.bundle/selected"] forState:UIControlStateNormal];
[selectPhotoButton addTarget:self action:@selector(photoSelected:) forControlEvents:UIControlEventTouchUpInside];
[overlayView addSubview:selectPhotoButton];
UIButton *cancelSelectPhotoButton = [[UIButton alloc]initWithFrame:CGRectMake(5, 20, 32, 32)];
[cancelSelectPhotoButton setImage:[UIImage imageNamed:@"PKImageBundle.bundle/cancel"] forState:UIControlStateNormal];
[cancelSelectPhotoButton addTarget:self action:@selector(cancelSelectedPhoto:) forControlEvents:UIControlEventTouchUpInside];
[overlayView addSubview:cancelSelectPhotoButton];
}
-(void)viewWillAppear:(BOOL)animated
{
[self.captureSession startRunning];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[self.captureSession stopRunning];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)capturePhoto:(id)sender
{
self.isCapturingImage = YES;
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in _stillImageOutput.connections)
{
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
if (imageSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *capturedImage = [[UIImage alloc]initWithData:imageData scale:1];
self.isCapturingImage = NO;
self.capturedImageView.image = capturedImage;
self.selectedImage = capturedImage;
imageData = nil;
if ([self shouldSkipImageConfirmation]) {
[self dismissViewControllerAnimated:YES completion:^{
if ([self.delegate respondsToSelector:@selector(imageSelected:)]) {
[self.delegate imageSelected:self.selectedImage];
}
[self.imageSelectedView removeFromSuperview];
}];
} else {
[self.view addSubview:self.imageSelectedView];
}
}
}];
}
-(IBAction)flash:(UIButton*)sender
{
if ([self.captureDevice isFlashAvailable]) {
if (self.captureDevice.flashActive) {
if([self.captureDevice lockForConfiguration:nil]) {
self.captureDevice.flashMode = AVCaptureFlashModeOff;
[sender setTintColor:[UIColor grayColor]];
[sender setSelected:NO];
}
}
else {
if([self.captureDevice lockForConfiguration:nil]) {
self.captureDevice.flashMode = AVCaptureFlashModeOn;
[sender setTintColor:[UIColor blueColor]];
[sender setSelected:YES];
}
}
[self.captureDevice unlockForConfiguration];
}
}
-(IBAction)showFrontCamera:(id)sender
{
if (self.isCapturingImage != YES) {
if (self.captureDevice == [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][0]) {
// rear active, switch to front
self.captureDevice = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][1];
[self.captureSession beginConfiguration];
AVCaptureDeviceInput * newInput = [AVCaptureDeviceInput deviceInputWithDevice:self.captureDevice error:nil];
for (AVCaptureInput * oldInput in self.captureSession.inputs) {
[self.captureSession removeInput:oldInput];
}
[self.captureSession addInput:newInput];
[self.captureSession commitConfiguration];
}
else if (self.captureDevice == [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][1]) {
// front active, switch to rear
self.captureDevice = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][0];
[self.captureSession beginConfiguration];
AVCaptureDeviceInput * newInput = [AVCaptureDeviceInput deviceInputWithDevice:self.captureDevice error:nil];
for (AVCaptureInput * oldInput in self.captureSession.inputs) {
[self.captureSession removeInput:oldInput];
}
[self.captureSession addInput:newInput];
[self.captureSession commitConfiguration];
}
// Need to reset flash btn
}
}
-(IBAction)showalbum:(id)sender
{
[self presentViewController:self.picker animated:YES completion:nil];
//
}
-(IBAction)photoSelected:(id)sender
{
[self dismissViewControllerAnimated:YES completion:^{
if ([self.delegate respondsToSelector:@selector(imageSelected:)]) {
[self.delegate imageSelected:self.selectedImage];
}
[self.imageSelectedView removeFromSuperview];
}];
}
-(IBAction)cancelSelectedPhoto:(id)sender
{
[self.imageSelectedView removeFromSuperview];
}
-(IBAction)cancel:(id)sender
{
[self dismissViewControllerAnimated:YES completion:^{
if ([self.delegate respondsToSelector:@selector(imageSelectionCancelled)]) {
[self.delegate imageSelectionCancelled];
}
}];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
if ([self shouldSkipImageConfirmation]) {
[self dismissViewControllerAnimated:NO completion:^{
[self dismissViewControllerAnimated:YES completion:^{
if ([self.delegate respondsToSelector:@selector(imageSelected:)]) {
[self.delegate imageSelected:self.selectedImage];
}
[self.imageSelectedView removeFromSuperview];
}];
}];
} else {
[self dismissViewControllerAnimated:YES completion:^{
self.capturedImageView.image = self.selectedImage;
[self.view addSubview:self.imageSelectedView];
}];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end