4

After the view is dismissed in a navigation controller the app crashes is it possible to reload the cell during a navigation controller unwind.

After the a segue if the flip button is tapped the camera changes but when the camera button is tapped the app crashes with this error

No active and enabled video connection

What could possibly be going wrong

import UIKit
import AVFoundation


protocol previewSegueDelegate {
func previewSegueDelegate(image:UIImage,device:AVCaptureDevice)
}

class MainCameraCollectionViewCell: UICollectionViewCell {

var gdelegate: gestureDelegate?
var pdelegate: previewSegueDelegate?


@IBOutlet weak var myView: UIView!

var captureSession = AVCaptureSession()
private var sessionQueue: DispatchQueue!
var captureConnection = AVCaptureConnection()
var currentCamera: AVCaptureDevice?

var photoOutPut: AVCapturePhotoOutput?

var cameraPreviewLayer: AVCaptureVideoPreviewLayer?

var image: UIImage?

 var usingFrontCamera = false

override func awakeFromNib() {
    super.awakeFromNib()
    setupCaptureSession()
    setupDevice()
    setupInput()
    setupPreviewLayer()
    startRunningCaptureSession()
    print("Inside of camera cell")
}

func setupCaptureSession(){
    captureSession.sessionPreset = AVCaptureSession.Preset.photo
    sessionQueue = DispatchQueue(label: "session queue")
}

func setupDevice(usingFrontCamera:Bool = false){
    sessionQueue.async {
    let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
    let devices = deviceDiscoverySession.devices

    for device in devices{
        if usingFrontCamera && device.position == AVCaptureDevice.Position.front {
            self.currentCamera = device
        } else if device.position == AVCaptureDevice.Position.back {
            self.currentCamera = device
        }
    }
    }
}

func setupInput() {
    sessionQueue.async {
        do {
            let captureDeviceInput = try AVCaptureDeviceInput(device: self.currentCamera!)
            if self.captureSession.canAddInput(captureDeviceInput) {
                self.captureSession.addInput(captureDeviceInput)
            }
            self.photoOutPut = AVCapturePhotoOutput()
            self.photoOutPut?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
            if self.captureSession.canAddOutput(self.photoOutPut!) {
                self.captureSession.addOutput(self.photoOutPut!)
            }
        } catch {
            print(error)
        }
    }
}
func setupPreviewLayer(){
    cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
    cameraPreviewLayer?.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
    self.layer.insertSublayer(cameraPreviewLayer!, at: 0)
}

func startRunningCaptureSession(){
    captureSession.startRunning()
}


@IBAction func cameraButton_TouchUpInside(_ sender: Any) {
    let settings = AVCapturePhotoSettings()
    photoOutPut?.capturePhoto(with: settings, delegate: self as AVCapturePhotoCaptureDelegate)
    print("camera button touched")
}

@IBAction func FlipThe_camera(_ sender: UIButton) {
print("Flip Touched")
    captureSession.beginConfiguration()
    if let inputs = captureSession.inputs as? [AVCaptureDeviceInput] {
        for input in inputs {
            captureSession.removeInput(input)
        }
    }
    usingFrontCamera = !usingFrontCamera
    setupCaptureSession()
    setupDevice(usingFrontCamera: usingFrontCamera)
    setupInput()
    captureSession.commitConfiguration()
    startRunningCaptureSession()
}

 }


  extension MainCameraCollectionViewCell: 
  AVCapturePhotoCaptureDelegate{
func photoOutput(_ output: AVCapturePhotoOutput, 
  didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    if let imageData = photo.fileDataRepresentation(){
        print(imageData)
        image = UIImage(data: imageData)
        if(self.image == nil){
            print("The image is empty")
        }
        pdelegate?.previewSegueDelegate(image: self.image!, device: 
      currentCamera!)
      }
    }
   }

1 Answers1

1

I believe you need to use:

captureSesssion.sessionPreset = AVCaptureSessionPresetPhoto

Answer found HERE by Aleksey Timoshchenko

    import UIKit
import AVFoundation

class ViewController: UIViewController, AVCapturePhotoCaptureDelegate {

var captureSesssion : AVCaptureSession!
var cameraOutput : AVCapturePhotoOutput!
var previewLayer : AVCaptureVideoPreviewLayer!

@IBOutlet weak var capturedImage: UIImageView!
@IBOutlet weak var previewView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    captureSesssion = AVCaptureSession()
    captureSesssion.sessionPreset = AVCaptureSessionPresetPhoto
    cameraOutput = AVCapturePhotoOutput()

    let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

    if let input = try? AVCaptureDeviceInput(device: device) {
        if (captureSesssion.canAddInput(input)) {
            captureSesssion.addInput(input)
            if (captureSesssion.canAddOutput(cameraOutput)) {
                captureSesssion.addOutput(cameraOutput)
                previewLayer = AVCaptureVideoPreviewLayer(session: captureSesssion)
                previewLayer.frame = previewView.bounds
                previewView.layer.addSublayer(previewLayer)
                captureSesssion.startRunning()
            }
        } else {
            print("issue here : captureSesssion.canAddInput")
        }
    } else {
        print("some problem here")
    }
}

// Take picture button
@IBAction func didPressTakePhoto(_ sender: UIButton) {
    let settings = AVCapturePhotoSettings()
    let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
    let previewFormat = [
         kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
         kCVPixelBufferWidthKey as String: 160,
         kCVPixelBufferHeightKey as String: 160
    ]
    settings.previewPhotoFormat = previewFormat
    cameraOutput.capturePhoto(with: settings, delegate: self)
}

// callBack from take picture
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

    if let error = error {
        print("error occure : \(error.localizedDescription)")
    }

    if  let sampleBuffer = photoSampleBuffer,
        let previewBuffer = previewPhotoSampleBuffer,
        let dataImage =  AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer:  sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {
        print(UIImage(data: dataImage)?.size as Any)

        let dataProvider = CGDataProvider(data: dataImage as CFData)
        let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
        let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)

        self.capturedImage.image = image
    } else {
        print("some error here")
    }
}

// This method you can use somewhere you need to know camera permission   state
func askPermission() {
    print("here")
    let cameraPermissionStatus =  AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)

    switch cameraPermissionStatus {
    case .authorized:
        print("Already Authorized")
    case .denied:
        print("denied")

        let alert = UIAlertController(title: "Sorry :(" , message: "But  could you please grant permission for camera within device settings",  preferredStyle: .alert)
        let action = UIAlertAction(title: "Ok", style: .cancel,  handler: nil)
        alert.addAction(action)
        present(alert, animated: true, completion: nil)

    case .restricted:
        print("restricted")
    default:
        AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: {
            [weak self]
            (granted :Bool) -> Void in

            if granted == true {
                // User granted
                print("User granted")
 DispatchQueue.main.async(){
            //Do smth that you need in main thread   
            } 
            }
            else {
                // User Rejected
                print("User Rejected")

DispatchQueue.main.async(){
            let alert = UIAlertController(title: "WHY?" , message:  "Camera it is the main feature of our application", preferredStyle: .alert)
                let action = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
                alert.addAction(action)
                self?.present(alert, animated: true, completion: nil)  
            } 
            }
        });
    }
}
}
Jake
  • 2,126
  • 1
  • 10
  • 23
  • 1
    Thank you for looking for our answer, but unfortunately we have already configured our `captureSession.sessionPreset = AVCaptureSession.Preset.Photo` inside of our `setupCaptureSession()` function and we still receive the same error. – TheRedCamaro3.0 3.0 Feb 15 '18 at 22:16
  • Sorry, I missed that. – Jake Feb 15 '18 at 22:23
  • 1
    Its all ok no reason to apologize, I cant figure out the cause of the error, it really has me stumped – TheRedCamaro3.0 3.0 Feb 15 '18 at 22:24
  • you're running on what device? – Jake Feb 15 '18 at 22:29
  • On an iPhone X and an iPhone 6S – TheRedCamaro3.0 3.0 Feb 15 '18 at 22:30
  • in ` func setupDevice(usingFrontCamera:Bool = false)` Try something like `if usingFrontCamera { AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front) } else {AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .back) } I'm wondering if that will make a difference – Jake Feb 15 '18 at 22:52
  • You know, Instead of creating the the default with unspecified and then editing it. – Jake Feb 15 '18 at 22:55
  • I just tried it, I removed all other code from `setupDevice(usingFrontCamera:Bool = false)` and added ` if usingFrontCamera { self.currentCamera = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .front) } else { self.currentCamera = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .back) }` but still received the same error. – TheRedCamaro3.0 3.0 Feb 15 '18 at 23:23
  • Have you added a breakpoint to the take picture function and stepped through to see exactly where it fails? – Jake Feb 15 '18 at 23:25
  • Ive tried inserting break points but it is a bit difficult to place them because the error occurs once the code has run through either for the front camera or the back camera then it segues then the segue dismisses and I try to flip the camera and run through the code again. – TheRedCamaro3.0 3.0 Feb 15 '18 at 23:35