-
-
Save sysnajar/3f3b0c36ff78128ee7d2ae4249201e6f to your computer and use it in GitHub Desktop.
This is the example of camera view for iOS written in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment