switching camera not working

Hi,


I am using XCode 7.3 with swift version 1.2 to build a custom camera app. I need to create one switch button for front and back camera. On running the app, it displays result from one camera okay but as soon as I touch the switch button it doesn't fetch input from the front camera and displays white blank screen.


Code that I am using is like below:



import UIKit

import AVFoundation

class ViewController: UIViewController {

@IBOutlet weak var cameraView: UIView!

var captureSession = AVCaptureSession()

var sessionOutput = AVCaptureStillImageOutput()

var previewLayer = AVCaptureVideoPreviewLayer()

let screenSize:CGRect = UIScreen.mainScreen().bounds

enum cameraType{

case Back

case Front

}

var frontCamera = false

var cameraPosition = AVCaptureDevicePosition.Back

override func viewDidLoad() {

super.viewDidLoad()

reloadCamera()

}


override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

}

override func prefersStatusBarHidden() -> Bool {

return true

}

@IBAction func takePhoto(sender: AnyObject) {

if let videoConnection = sessionOutput.connectionWithMediaType(AVMediaTypeVideo){

sessionOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {

buffer, error in

let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)

UIImageWriteToSavedPhotosAlbum(UIImage(data:imageData)!, nil, nil, nil)

})

}

}

@IBAction func switchCamera(sender: AnyObject) {

frontCamera = !frontCamera

captureSession.stopRunning()

previewLayer.removeFromSuperlayer()

captureSession.commitConfiguration()

reloadCamera()

}

func reloadCamera(){

var captureDevice:AVCaptureDevice! = nil

if (frontCamera == true) {

let videoDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)

for device in videoDevices{

let device = device as! AVCaptureDevice

if device.position == AVCaptureDevicePosition.Front {

captureDevice = device

break

}

}

} else {

captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

}

do{

let input = try AVCaptureDeviceInput(device: captureDevice)

captureSession.stopRunning()

previewLayer.removeFromSuperlayer()

captureSession.removeInput(input)

captureSession.removeOutput(sessionOutput)

if captureSession.canAddInput(input){

captureSession.addInput(input)

sessionOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]

if captureSession.canAddOutput(sessionOutput){

captureSession.addOutput(sessionOutput)

captureSession.startRunning()

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait

cameraView.layer.addSublayer(previewLayer)

previewLayer.frame = CGRectMake(0, 0, screenSize.width , screenSize.height)

}

}

}

catch{

print("Error")

}

}


}

Please look at the AVCam sample code, which teaches you how to switch between cameras.


https://developer.apple.com/library/content/samplecode/AVCam/Introduction/Intro.html

This example is using swift 3 where AVCaptureDeviceDiscoverySession is introduced that doesn't support in XCode version. Is there any simple solution swift 1.2 ?

switching camera not working
 
 
Q