-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLSimpleCamera.m
More file actions
756 lines (617 loc) · 25.4 KB
/
LLSimpleCamera.m
File metadata and controls
756 lines (617 loc) · 25.4 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//
// CameraViewController.m
// LLSimpleCamera
//
// Created by Ömer Faruk Gül on 24/10/14.
// Copyright (c) 2014 Ömer Faruk Gül. All rights reserved.
//
#import "LLSimpleCamera.h"
#import <ImageIO/CGImageProperties.h>
#import "UIImage+FixOrientation.h"
@interface LLSimpleCamera () <AVCaptureFileOutputRecordingDelegate>
@property (strong, nonatomic) UIView *preview;
@property (strong, nonatomic) AVCaptureStillImageOutput *stillImageOutput;
@property (strong, nonatomic) AVCaptureSession *session;
@property (strong, nonatomic) AVCaptureDevice *videoCaptureDevice;
@property (strong, nonatomic) AVCaptureDevice *audioCaptureDevice;
@property (strong, nonatomic) AVCaptureDeviceInput *videoDeviceInput;
@property (strong, nonatomic) AVCaptureDeviceInput *audioDeviceInput;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
@property (strong, nonatomic) UITapGestureRecognizer *tapGesture;
@property (strong, nonatomic) CALayer *focusBoxLayer;
@property (strong, nonatomic) CAAnimation *focusBoxAnimation;
@property (strong, nonatomic) AVCaptureMovieFileOutput *movieFileOutput;
@property (nonatomic, copy) void (^didRecord)(LLSimpleCamera *camera, NSURL *outputFileUrl, NSError *error);
@end
NSString *const LLSimpleCameraErrorDomain = @"LLSimpleCameraErrorDomain";
@implementation LLSimpleCamera
- (instancetype)init {
self = [self initWithVideoEnabled:NO];
if(self) {
}
return self;
}
- (instancetype)initWithVideoEnabled:(BOOL)videoEnabled {
self = [self initWithQuality:AVCaptureSessionPresetHigh position:CameraPositionBack videoEnabled:videoEnabled];
if(self) {
}
return self;
}
- (instancetype)initWithQuality:(NSString *)quality position:(CameraPosition)position videoEnabled:(BOOL)videoEnabled {
self = [super initWithNibName:nil bundle:nil];
if(self) {
_cameraQuality = quality;
_position = position;
_fixOrientationAfterCapture = NO;
_tapToFocus = YES;
_useDeviceOrientation = NO;
_flash = CameraFlashOff;
_videoEnabled = videoEnabled;
_recording = NO;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.view.autoresizingMask = UIViewAutoresizingNone;
self.preview = [[UIView alloc] initWithFrame:CGRectZero];
self.preview.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.preview];
// tap to focus
self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(previewTapped:)];
self.tapGesture.numberOfTapsRequired = 1;
[self.tapGesture setDelaysTouchesEnded:NO];
[self.preview addGestureRecognizer:self.tapGesture];
// add focus box to view
[self addDefaultFocusBox];
}
- (void)addDefaultFocusBox {
CALayer *focusBox = [[CALayer alloc] init];
focusBox.cornerRadius = 5.0f;
focusBox.bounds = CGRectMake(0.0f, 0.0f, 70, 60);
focusBox.borderWidth = 3.0f;
focusBox.borderColor = [[UIColor yellowColor] CGColor];
focusBox.opacity = 0.0f;
[self.view.layer addSublayer:focusBox];
CABasicAnimation *focusBoxAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
focusBoxAnimation.duration = 0.75;
focusBoxAnimation.autoreverses = NO;
focusBoxAnimation.repeatCount = 0.0;
focusBoxAnimation.fromValue = [NSNumber numberWithFloat:1.0];
focusBoxAnimation.toValue = [NSNumber numberWithFloat:0.0];
[self alterFocusBox:focusBox animation:focusBoxAnimation];
}
- (void)alterFocusBox:(CALayer *)layer animation:(CAAnimation *)animation {
self.focusBoxLayer = layer;
self.focusBoxAnimation = animation;
}
- (void)attachToViewController:(UIViewController *)vc withFrame:(CGRect)frame {
[vc.view addSubview:self.view];
[vc addChildViewController:self];
[self didMoveToParentViewController:vc];
vc.view.frame = frame;
}
# pragma mark Touch Delegate
- (void) previewTapped: (UIGestureRecognizer *) gestureRecognizer
{
if(!self.tapToFocus) {
return;
}
CGPoint touchedPoint = (CGPoint) [gestureRecognizer locationInView:self.preview];
// focus
CGPoint pointOfInterest = [self convertToPointOfInterestFromViewCoordinates:touchedPoint];
[self focusAtPoint:pointOfInterest];
// show the box
[self showFocusBox:touchedPoint];
}
#pragma mark Camera Actions
- (void)start {
[LLSimpleCamera requestCameraPermission:^(BOOL granted) {
if(granted) {
// request microphone permission if video is enabled
if(self.videoEnabled) {
[LLSimpleCamera requestMicrophonePermission:^(BOOL granted) {
if(granted) {
[self initialize];
}
else {
NSError *error = [NSError errorWithDomain:LLSimpleCameraErrorDomain
code:LLSimpleCameraErrorCodeMicrophonePermission
userInfo:nil];
if(self.onError) {
self.onError(self, error);
}
}
}];
}
else {
[self initialize];
}
}
else {
NSError *error = [NSError errorWithDomain:LLSimpleCameraErrorDomain
code:LLSimpleCameraErrorCodeCameraPermission
userInfo:nil];
if(self.onError) {
self.onError(self, error);
}
}
}];
}
- (void)initialize {
if(!_session) {
_session = [[AVCaptureSession alloc] init];
_session.sessionPreset = self.cameraQuality;
// preview layer
CGRect bounds = self.preview.layer.bounds;
_captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
_captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_captureVideoPreviewLayer.bounds = bounds;
_captureVideoPreviewLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
[self.preview.layer addSublayer:_captureVideoPreviewLayer];
AVCaptureDevicePosition devicePosition;
switch (self.position) {
case CameraPositionBack:
devicePosition = AVCaptureDevicePositionBack;
break;
case CameraPositionFront:
devicePosition = AVCaptureDevicePositionFront;
break;
default:
devicePosition = AVCaptureDevicePositionUnspecified;
break;
}
if(devicePosition == AVCaptureDevicePositionUnspecified) {
_videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
else {
_videoCaptureDevice = [self cameraWithPosition:devicePosition];
}
NSError *error = nil;
_videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:_videoCaptureDevice error:&error];
if (!_videoDeviceInput) {
if(self.onError) {
self.onError(self, error);
}
return;
}
if([self.session canAddInput:_videoDeviceInput]) {
[self.session addInput:_videoDeviceInput];
}
// add audio if video is enabled
if(self.videoEnabled) {
_audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
_audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:_audioCaptureDevice error:&error];
if (!_audioDeviceInput) {
if(self.onError) {
self.onError(self, error);
}
}
if([self.session canAddInput:_audioDeviceInput]) {
[self.session addInput:_audioDeviceInput];
}
_movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
if([self.session canAddOutput:_movieFileOutput]) {
[self.session addOutput:_movieFileOutput];
}
}
self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[self.stillImageOutput setOutputSettings:outputSettings];
[self.session addOutput:self.stillImageOutput];
}
//if we had disabled the connection on capture, re-enable it
if (![self.captureVideoPreviewLayer.connection isEnabled]) {
[self.captureVideoPreviewLayer.connection setEnabled:YES];
}
[self.session startRunning];
}
- (void)stop {
[self.session stopRunning];
}
#pragma mark Image Methods
-(void)capture:(void (^)(LLSimpleCamera *camera, UIImage *image, NSDictionary *metadata, NSError *error))onCapture exactSeenImage:(BOOL)exactSeenImage {
if(!self.session) {
NSError *error = [NSError errorWithDomain:LLSimpleCameraErrorDomain
code:LLSimpleCameraErrorCodeSession
userInfo:nil];
onCapture(self, nil, nil, error);
return;
}
// get connection and set orientation
AVCaptureConnection *videoConnection = [self captureConnection];
videoConnection.videoOrientation = [self orientationForConnection];
// freeze the screen
[self.captureVideoPreviewLayer.connection setEnabled:NO];
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
UIImage *image = nil;
NSDictionary *metadata = nil;
// check if we got the image buffer
if (imageSampleBuffer != NULL) {
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if(exifAttachments) {
metadata = (__bridge NSDictionary*)exifAttachments;
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
image = [[UIImage alloc] initWithData:imageData];
if(exactSeenImage) {
image = [self cropImageUsingPreviewBounds:image];
}
if(self.fixOrientationAfterCapture) {
image = [image fixOrientation];
}
}
// trigger the block
if(onCapture) {
onCapture(self, image, metadata, error);
}
}];
}
-(void)capture:(void (^)(LLSimpleCamera *camera, UIImage *image, NSDictionary *metadata, NSError *error))onCapture {
[self capture:onCapture exactSeenImage:NO];
}
#pragma mark Video Methods
- (void)startRecordingWithOutputUrl:(NSURL *)url {
// check if video is enabled
if(!self.videoEnabled) {
NSError *error = [NSError errorWithDomain:LLSimpleCameraErrorDomain
code:LLSimpleCameraErrorCodeVideoNotEnabled
userInfo:nil];
if(self.onError) {
self.onError(self, error);
}
return;
}
if(self.flash == CameraFlashOn) {
[self enableTorch:YES];
}
[self.movieFileOutput startRecordingToOutputFileURL:url recordingDelegate:self];
}
- (void)stopRecording:(void (^)(LLSimpleCamera *camera, NSURL *outputFileUrl, NSError *error))completionBlock {
if(!self.videoEnabled) {
return;
}
self.didRecord = completionBlock;
[self.movieFileOutput stopRecording];
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections {
self.recording = YES;
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {
self.recording = NO;
[self enableTorch:NO];
if(self.didRecord) {
self.didRecord(self, outputFileURL, error);
}
}
- (void)enableTorch:(BOOL)enabled {
// check if the device has a toch, otherwise don't even bother to take any action.
if([self isTorchAvailable]) {
[self.session beginConfiguration];
[self.videoCaptureDevice lockForConfiguration:nil];
if (enabled) {
[self.videoCaptureDevice setTorchMode:AVCaptureTorchModeOn];
} else {
[self.videoCaptureDevice setTorchMode:AVCaptureTorchModeOff];
}
[self.videoCaptureDevice unlockForConfiguration];
[self.session commitConfiguration];
}
}
#pragma mark Helper Methods
- (UIImage *)cropImageUsingPreviewBounds:(UIImage *)image {
CGRect previewBounds = self.captureVideoPreviewLayer.bounds;
CGRect outputRect = [self.captureVideoPreviewLayer metadataOutputRectOfInterestForRect:previewBounds];
CGImageRef takenCGImage = image.CGImage;
size_t width = CGImageGetWidth(takenCGImage);
size_t height = CGImageGetHeight(takenCGImage);
CGRect cropRect = CGRectMake(outputRect.origin.x * width, outputRect.origin.y * height,
outputRect.size.width * width, outputRect.size.height * height);
CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, cropRect);
image = [UIImage imageWithCGImage:cropCGImage scale:1 orientation:image.imageOrientation];
CGImageRelease(cropCGImage);
return image;
}
- (AVCaptureConnection *)captureConnection {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in self.stillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
return videoConnection;
}
- (void)setVideoCaptureDevice:(AVCaptureDevice *)videoCaptureDevice {
_videoCaptureDevice = videoCaptureDevice;
if(videoCaptureDevice.flashMode == AVCaptureFlashModeAuto) {
_flash = CameraFlashAuto;
}
else if(videoCaptureDevice.flashMode == AVCaptureFlashModeOn) {
_flash = CameraFlashOn;
}
else if(videoCaptureDevice.flashMode == AVCaptureFlashModeOff) {
_flash = CameraFlashOff;
}
else {
_flash = CameraFlashOff;
}
// trigger block
if(self.onDeviceChange) {
self.onDeviceChange(self, videoCaptureDevice);
}
}
- (BOOL)isFlashAvailable {
return self.videoCaptureDevice.hasFlash && self.videoCaptureDevice.isFlashAvailable;
}
- (BOOL)isTorchAvailable {
return self.videoCaptureDevice.hasTorch && self.videoCaptureDevice.isTorchAvailable;
}
- (BOOL)updateFlashMode:(CameraFlash)cameraFlash {
if(!self.session)
return NO;
AVCaptureFlashMode flashMode;
if(cameraFlash == CameraFlashOn) {
flashMode = AVCaptureFlashModeOn;
}
else if(cameraFlash == CameraFlashAuto) {
flashMode = AVCaptureFlashModeAuto;
}
else {
flashMode = AVCaptureFlashModeOff;
}
if([self.videoCaptureDevice isFlashModeSupported:flashMode]) {
NSError *error;
if([self.videoCaptureDevice lockForConfiguration:&error]) {
self.videoCaptureDevice.flashMode = flashMode;
[self.videoCaptureDevice unlockForConfiguration];
_flash = cameraFlash;
return YES;
}
else {
if(self.onError) {
self.onError(self, error);
}
return NO;
}
}
else {
return NO;
}
}
- (CameraPosition)togglePosition {
if(!self.session) {
return self.position;
}
if(self.position == CameraPositionBack) {
self.cameraPosition = CameraPositionFront;
}
else {
self.cameraPosition = CameraPositionBack;
}
return self.position;
}
- (void)setCameraPosition:(CameraPosition)cameraPosition
{
if(_position == cameraPosition || !self.session) {
return;
}
[self.session beginConfiguration];
// remove existing input
[self.session removeInput:self.videoDeviceInput];
// get new input
AVCaptureDevice *device = nil;
if(self.videoDeviceInput.device.position == AVCaptureDevicePositionBack) {
device = [self cameraWithPosition:AVCaptureDevicePositionFront];
}
else {
device = [self cameraWithPosition:AVCaptureDevicePositionBack];
}
if(!device) {
return;
}
// add input to session
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
if(error) {
if(self.onError) {
self.onError(self, error);
}
[self.session commitConfiguration];
return;
}
_position = cameraPosition;
[self.session addInput:videoInput];
[self.session commitConfiguration];
self.videoCaptureDevice = device;
self.videoDeviceInput = videoInput;
}
// Find a camera with the specified AVCaptureDevicePosition, returning nil if one is not found
- (AVCaptureDevice *) cameraWithPosition:(AVCaptureDevicePosition) position
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if (device.position == position) return device;
}
return nil;
}
#pragma mark Focus
- (void) focusAtPoint:(CGPoint)point
{
//NSLog(@"Focusing at point %@", NSStringFromCGPoint(point));
AVCaptureDevice *device = self.videoCaptureDevice;
if (device.isFocusPointOfInterestSupported && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
NSError *error;
if ([device lockForConfiguration:&error]) {
device.focusPointOfInterest = point;
device.focusMode = AVCaptureFocusModeAutoFocus;
[device unlockForConfiguration];
}
if(error && self.onError) {
self.onError(self, error);
}
}
}
- (void)showFocusBox:(CGPoint)point {
if(self.focusBoxLayer) {
// clear animations
[self.focusBoxLayer removeAllAnimations];
// move layer to the touc point
[CATransaction begin];
[CATransaction setValue: (id) kCFBooleanTrue forKey: kCATransactionDisableActions];
self.focusBoxLayer.position = point;
[CATransaction commit];
}
if(self.focusBoxAnimation) {
// run the animation
[self.focusBoxLayer addAnimation:self.focusBoxAnimation forKey:@"animateOpacity"];
}
}
- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates
{
AVCaptureVideoPreviewLayer *previewLayer = self.captureVideoPreviewLayer;
CGPoint pointOfInterest = CGPointMake(.5f, .5f);
CGSize frameSize = previewLayer.frame.size;
if ( [previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResize] ) {
pointOfInterest = CGPointMake(viewCoordinates.y / frameSize.height, 1.f - (viewCoordinates.x / frameSize.width));
} else {
CGRect cleanAperture;
for (AVCaptureInputPort *port in [self.videoDeviceInput ports]) {
if (port.mediaType == AVMediaTypeVideo) {
cleanAperture = CMVideoFormatDescriptionGetCleanAperture([port formatDescription], YES);
CGSize apertureSize = cleanAperture.size;
CGPoint point = viewCoordinates;
CGFloat apertureRatio = apertureSize.height / apertureSize.width;
CGFloat viewRatio = frameSize.width / frameSize.height;
CGFloat xc = .5f;
CGFloat yc = .5f;
if ( [previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResizeAspect] ) {
if (viewRatio > apertureRatio) {
CGFloat y2 = frameSize.height;
CGFloat x2 = frameSize.height * apertureRatio;
CGFloat x1 = frameSize.width;
CGFloat blackBar = (x1 - x2) / 2;
if (point.x >= blackBar && point.x <= blackBar + x2) {
xc = point.y / y2;
yc = 1.f - ((point.x - blackBar) / x2);
}
} else {
CGFloat y2 = frameSize.width / apertureRatio;
CGFloat y1 = frameSize.height;
CGFloat x2 = frameSize.width;
CGFloat blackBar = (y1 - y2) / 2;
if (point.y >= blackBar && point.y <= blackBar + y2) {
xc = ((point.y - blackBar) / y2);
yc = 1.f - (point.x / x2);
}
}
} else if ([previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResizeAspectFill]) {
if (viewRatio > apertureRatio) {
CGFloat y2 = apertureSize.width * (frameSize.width / apertureSize.height);
xc = (point.y + ((y2 - frameSize.height) / 2.f)) / y2;
yc = (frameSize.width - point.x) / frameSize.width;
} else {
CGFloat x2 = apertureSize.height * (frameSize.height / apertureSize.width);
yc = 1.f - ((point.x + ((x2 - frameSize.width) / 2)) / x2);
xc = point.y / frameSize.height;
}
}
pointOfInterest = CGPointMake(xc, yc);
break;
}
}
}
return pointOfInterest;
}
#pragma mark - Controller Lifecycle
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// NSLog(@"layout cameraVC : %d", self.interfaceOrientation);
self.preview.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
CGRect bounds = self.preview.bounds;
self.captureVideoPreviewLayer.bounds = bounds;
self.captureVideoPreviewLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
self.captureVideoPreviewLayer.connection.videoOrientation = [self orientationForConnection];
}
- (AVCaptureVideoOrientation)orientationForConnection
{
AVCaptureVideoOrientation videoOrientation = AVCaptureVideoOrientationPortrait;
if(self.useDeviceOrientation) {
switch ([UIDevice currentDevice].orientation) {
case UIDeviceOrientationLandscapeLeft:
// yes we to the right, this is not bug!
videoOrientation = AVCaptureVideoOrientationLandscapeRight;
break;
case UIDeviceOrientationLandscapeRight:
videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
break;
case UIDeviceOrientationPortraitUpsideDown:
videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
break;
default:
videoOrientation = AVCaptureVideoOrientationPortrait;
break;
}
}
else {
switch (self.interfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
break;
case UIInterfaceOrientationLandscapeRight:
videoOrientation = AVCaptureVideoOrientationLandscapeRight;
break;
case UIInterfaceOrientationPortraitUpsideDown:
videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
break;
default:
videoOrientation = AVCaptureVideoOrientationPortrait;
break;
}
}
return videoOrientation;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark static methods
+ (void)requestCameraPermission:(void (^)(BOOL granted))completionBlock {
if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
// return to main thread
dispatch_async(dispatch_get_main_queue(), ^{
if(completionBlock) {
completionBlock(granted);
}
});
}];
} else {
completionBlock(YES);
}
}
+ (void)requestMicrophonePermission:(void (^)(BOOL granted))completionBlock {
if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
// return to main thread
dispatch_async(dispatch_get_main_queue(), ^{
if(completionBlock) {
completionBlock(granted);
}
});
}];
}
}
@end