Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import AVFoundation
- class ViewController: UIViewController, CLLocationManagerDelegate {
- @IBOutlet weak var takePhotoButton: UIButton!
- let captureSession = AVCaptureSession()
- var previewLayer: AVCaptureVideoPreviewLayer ?
- // If we find a device we'll store it here for later use
- var captureDevice: AVCaptureDevice ?
- func focusTo(value: Float) {
- if let device = captureDevice {
- if (device.lockForConfiguration(nil)) {
- device.setFocusModeLockedWithLensPosition(value, completionHandler: {
- (time) - > Void in
- })
- device.unlockForConfiguration()
- }
- }
- }
- @IBAction func takePhotoButtonPressed(sender: UIButton) {
- captureSession.sessionPreset = AVCaptureSessionPresetHigh
- let devices = AVCaptureDevice.devices()
- // Loop through all the capture devices on this phone
- for device in devices {
- // Make sure this particular device supports video
- if (device.hasMediaType(AVMediaTypeVideo)) {
- // Finally check the position and confirm we've got the back camera
- if (device.position == AVCaptureDevicePosition.Back) {
- captureDevice = device as ? AVCaptureDevice
- if captureDevice != nil {
- println("Capture device found")
- beginSession()
- }
- }
- }
- }
- }
- let screenWidth = UIScreen.mainScreen().bounds.size.width
- override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
- var anyTouch = touches.anyObject() as UITouch
- var touchPercent = anyTouch.locationInView(self.view).x / screenWidth
- focusTo(Float(touchPercent))
- }
- override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
- var anyTouch = touches.anyObject() as UITouch
- var touchPercent = anyTouch.locationInView(self.view).x / screenWidth
- focusTo(Float(touchPercent))
- }
- func configureDevice() {
- if let device = captureDevice {
- device.lockForConfiguration(nil)
- device.focusMode = .Locked
- device.unlockForConfiguration()
- }
- }
- func beginSession() {
- configureDevice()
- var err: NSError ? = nil
- captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: & err))
- if err != nil {
- println("error: (err?.localizedDescription)")
- }
- previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
- self.view.layer.addSublayer(previewLayer)
- previewLayer ? .frame = self.view.layer.frame
- captureSession.startRunning()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement