0

I am trying to make a selfie app. I am unable to access the front camera. When I run the below code, the app still picks only the back camera.I am working on Xcode 7.1.1. I have also tried it on Xcode 7.2 Beta. Can someone help ? Tried solutions given in the other question here, but that did not solve the problem.

import UIKit
import AVFoundation

class ViewController: UIViewController {




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


var captureSession: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var previewLayer: AVCaptureVideoPreviewLayer?

enum AVCaptureDevicePosition : Int {
case Unspecified
case Front
case Back
}


func viewDidAppear(animated: Bool){
    super.viewDidAppear(animated)
    previewLayer!.frame = previewView.bounds
    reloadCamera()

}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

    func reloadCamera() {

    captureSession = AVCaptureSession()
    captureSession!.sessionPreset = AVCaptureSessionPresetPhoto

    //let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    // access the front camera

    let videoDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
    var captureDevice:AVCaptureDevice

    for device in videoDevices{
        let device = device as AVCaptureDevice
        if device.position == AVCaptureDevicePosition.Front {

        captureDevice = device
        }
}





    var error: NSError?

    var input: AVCaptureDeviceInput!

    do {
        input = try AVCaptureDeviceInput(device : captureDevice)
    } catch let error1 as NSError {
        error = error1
        input = nil
}



    if error == nil && captureSession!.canAddInput(input) {
        captureSession!.addInput(input)

    stillImageOutput = AVCaptureStillImageOutput()
        stillImageOutput!.outputSettings = [AVVideoCodecKey :AVVideoCodecJPEG]

    if captureSession!.canAddOutput(stillImageOutput){
        captureSession!.addOutput(stillImageOutput)

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer!.videoGravity = AVLayerVideoGravityResizeAspect
        previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
        previewView.layer.addSublayer(previewLayer!)

        captureSession!.startRunning()

    }


}


}




@IBAction func didPressTakePhoto(sender: UIButton) {


    if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo){
        videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
            if (sampleBuffer != nil){

                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProviderCreateWithCFData(imageData)
                let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider,nil,true,CGColorRenderingIntent.RenderingIntentDefault)

                let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
                self.capturedImage.image = image
                UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
            }
    })
}

}

@IBAction func didPressTakeAnother(sender : AnyObject){
    captureSession!.startRunning()
}

}
Goutham
  • 9
  • 3
  • 1
    Possible duplicate of [How to get the front camera in Swift?](http://stackoverflow.com/questions/29155623/how-to-get-the-front-camera-in-swift) – NSNoob Nov 16 '15 at 13:39
  • I tried that and didn't work. Could this be something related to iOS 9.1 ? – Goutham Nov 17 '15 at 13:42

0 Answers0