فهرست منبع

1.修改iOS Apex Drivers第一次打开相机授权成功后未初始化Camera。

Pen Li 7 سال پیش
والد
کامیت
c98cbbb441
1فایلهای تغییر یافته به همراه46 افزوده شده و 6 حذف شده
  1. 46 6
      Redant Drivers/Apex And Drivers/Camera/RACameraViewController.m

+ 46 - 6
Redant Drivers/Apex And Drivers/Camera/RACameraViewController.m

@@ -49,8 +49,39 @@
     [self.takePhotoBtn setImage:normal_img forState:UIControlStateNormal];
     [self.takePhotoBtn setImage:highlight_img forState:UIControlStateHighlighted];
     
-    [self initCapture];
-    [self initTakePicture];
+//    [self initCapture];
+//    [self initTakePicture];
+    
+    if ([self camerAuthorization]) {
+        
+        [self initCapture];
+        [self initTakePicture];
+        
+    } else {
+        // fix:第一次打开相机的时候授权成功后,未初始化相机
+        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
+            
+            dispatch_async(dispatch_get_main_queue(), ^{
+                if (granted) {
+                    [self initCapture];
+                    [self initTakePicture];
+                    
+                    CGRect frame = self.previewContainer.bounds;
+                    [self resetPreview:CGRectOffset(frame, CGRectGetWidth(frame), 0)];
+                    
+                    [UIView animateWithDuration:0.3 animations:^{
+                        self.previewLayer.frame = frame;
+                    }];
+                    
+                    if (self.cameraInitial && !self.captureSession.isRunning) {
+                        [self.captureSession startRunning];
+                    }
+                    
+                }
+            });
+            
+        }];
+    }
 }
 
 - (void)didReceiveMemoryWarning {
@@ -61,7 +92,7 @@
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
     
-    if (self.cameraInitial) {
+    if (self.cameraInitial && !self.captureSession.isRunning) {
         [self.captureSession startRunning];
     }
 }
@@ -77,7 +108,17 @@
 - (void)viewDidLayoutSubviews {
     [super viewDidLayoutSubviews];
     
-    self.previewLayer.frame = self.previewContainer.bounds;
+    if ([self camerAuthorization] && self.cameraInitial) {
+        
+        [self resetPreview:self.previewContainer.bounds];
+    }
+}
+
+#pragma mark - Capture
+
+- (void)resetPreview:(CGRect)frame {
+    
+    self.previewLayer.frame = frame;
     
     if (self.previewLayer.connection.isVideoOrientationSupported) {
         self.previewLayer.connection.videoOrientation = [self captureVideoOrientation];
@@ -86,10 +127,9 @@
     if ([self.captureOutput connectionWithMediaType:AVMediaTypeVideo].isVideoOrientationSupported) {
         [self.captureOutput connectionWithMediaType:AVMediaTypeVideo].videoOrientation = [self captureVideoOrientation];
     }
+    
 }
 
-#pragma mark - Capture
-
 - (BOOL)camerAuthorization {
     
     AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];