21 Replies
      Latest reply on Sep 19, 2016 6:26 PM by sjheil87
      photogrammer Level 1 Level 1 (0 points)

        I would like to present a live camera view within the MessagesViewController. There are quite a few different camera APIs to choose from, for live preview I picked the below code which works fine in a standard app. (Tested on iOS 10 as well.)

         

        Which API should I be using to present a live camera preview?

         

        I have added the plist item for camera permission message.

         

        Thanks in advance!

         

         

        -(void)addLiveCamera:(UIView*)camView {
            self.cameraCaptureSession = [AVCaptureSession new];
            NSArray *availableCameraDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
            AVCaptureDevice *backCameraDevice=nil,*frontCameraDevice=nil;
            for( AVCaptureDevice *device in availableCameraDevices ) {
                if( device.position == AVCaptureDevicePositionBack ) {
                    backCameraDevice = device;
                } else if ( device.position == AVCaptureDevicePositionFront ) {
                    frontCameraDevice = device;
                }
            }
            NSError *error = nil;
            AVCaptureDeviceInput *cameraInput = [AVCaptureDeviceInput deviceInputWithDevice:frontCameraDevice error:&error];
            if( [self.cameraCaptureSession canAddInput:cameraInput] ) {
                [self.cameraCaptureSession addInput:cameraInput];
                AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.cameraCaptureSession];
                previewLayer.videoGravity = AVLayerVideoGravityResize;
                previewLayer.frame = camView.bounds;
                [camView.layer addSublayer:previewLayer];
                camView.backgroundColor = [UIColor clearColor];
                [self.cameraCaptureSession startRunning];
            }
        }