Skip to content

Instantly share code, notes, and snippets.

@MihaelIsaev
Created April 16, 2015 19:30
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MihaelIsaev/273e4e8ddaaf062d2155 to your computer and use it in GitHub Desktop.
Save MihaelIsaev/273e4e8ddaaf062d2155 to your computer and use it in GitHub Desktop.
This is the example of camera view for iOS written in Swift
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
@IBOutlet weak var myView: UIView!
var session: AVCaptureSession?
var device: AVCaptureDevice?
var input: AVCaptureDeviceInput?
var output: AVCaptureMetadataOutput?
var prevLayer: AVCaptureVideoPreviewLayer?
override func viewDidLoad() {
super.viewDidLoad()
createSession()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
prevLayer?.frame.size = myView.frame.size
}
func createSession() {
session = AVCaptureSession()
device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var error: NSError? = nil
input = AVCaptureDeviceInput(device: device, error: &error)
if error == nil {
session?.addInput(input)
} else {
println("camera input error: \(error)")
}
prevLayer = AVCaptureVideoPreviewLayer(session: session)
prevLayer?.frame.size = myView.frame.size
prevLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
prevLayer?.connection.videoOrientation = transformOrientation(UIInterfaceOrientation(rawValue: UIApplication.sharedApplication().statusBarOrientation.rawValue)!)
myView.layer.addSublayer(prevLayer)
session?.startRunning()
}
func cameraWithPosition(position: AVCaptureDevicePosition) -> AVCaptureDevice? {
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
for device in devices {
if device.position == position {
return device as? AVCaptureDevice
}
}
return nil
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animateAlongsideTransition({ (context) -> Void in
self.prevLayer?.connection.videoOrientation = self.transformOrientation(UIInterfaceOrientation(rawValue: UIApplication.sharedApplication().statusBarOrientation.rawValue)!)
self.prevLayer?.frame.size = self.myView.frame.size
}, completion: { (context) -> Void in
})
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}
func transformOrientation(orientation: UIInterfaceOrientation) -> AVCaptureVideoOrientation {
switch orientation {
case .LandscapeLeft:
return .LandscapeLeft
case .LandscapeRight:
return .LandscapeRight
case .PortraitUpsideDown:
return .PortraitUpsideDown
default:
return .Portrait
}
}
@IBAction func switchCameraSide(sender: AnyObject) {
if let sess = session {
let currentCameraInput: AVCaptureInput = sess.inputs[0] as! AVCaptureInput
sess.removeInput(currentCameraInput)
var newCamera: AVCaptureDevice
if (currentCameraInput as! AVCaptureDeviceInput).device.position == .Back {
newCamera = self.cameraWithPosition(.Front)!
} else {
newCamera = self.cameraWithPosition(.Back)!
}
let newVideoInput = AVCaptureDeviceInput(device: newCamera, error: nil)
session?.addInput(newVideoInput)
}
}
}
@MihaelIsaev
Copy link
Author

Wow guys I just noticed that you're making updates in the comments! You are awesome! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment