Advertisement
neatekFb

Get capture from camera to custom UIView

Nov 4th, 2016
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.94 KB | None | 0 0
  1. // Vladimir Zhelnov - neatek.pw - Web/iOS dev
  2. //
  3. //  Class_CameraView.swift
  4. //  iconbaseApp
  5. //
  6. //  Created by Vladimir on 04.11.16.
  7. //  Copyright © 2016 Vladimir Zhelnov. All rights reserved.
  8. //
  9.  
  10. import UIKit
  11. //import Haneke
  12. //import Kingfisher
  13. import AVFoundation
  14.  
  15. @available(iOS 10.0, *)
  16. class Class_CameraView: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  17.    
  18.     @IBOutlet weak var cameraView: UIView!
  19.    
  20.     var captureSession = AVCaptureSession();
  21.     var sessionOutput = AVCapturePhotoOutput();
  22.     var sessionOutputSetting = AVCapturePhotoSettings(format: [AVVideoCodecKey:AVVideoCodecJPEG]);
  23.     var previewLayer = AVCaptureVideoPreviewLayer();
  24.    
  25.     override func viewDidLoad() {
  26.         super.viewDidLoad()
  27.     }
  28.    
  29.     override func didReceiveMemoryWarning() {
  30.         super.didReceiveMemoryWarning()
  31.         // Dispose of any resources that can be recreated.
  32.     }
  33.    
  34.     override func viewWillDisappear(_ animated: Bool) {
  35.         super.viewWillDisappear(animated)
  36.         UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
  37.     }
  38.    
  39.     override func viewDidLayoutSubviews() {
  40.         super.viewDidLayoutSubviews()
  41.         previewLayer.frame = cameraView.bounds
  42.     }
  43.    
  44.     override func viewWillAppear(_ animated: Bool) {
  45.         let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.unspecified)
  46.         for device in (deviceDiscoverySession?.devices)! {
  47.             if(device.position == AVCaptureDevicePosition.front){
  48.                 do{
  49.                     let input = try AVCaptureDeviceInput(device: device)
  50.                     if(captureSession.canAddInput(input)){
  51.                         captureSession.addInput(input);
  52.                        
  53.                         if(captureSession.canAddOutput(sessionOutput)){
  54.                             captureSession.addOutput(sessionOutput);
  55.                             previewLayer = AVCaptureVideoPreviewLayer(session: captureSession);
  56.                             previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  57.                             previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait;
  58.                             cameraView.layer.addSublayer(previewLayer);
  59.                            
  60.                             captureSession.startRunning()
  61.                             print("Start Running")
  62.                         } else {
  63.                             print("Cant add output")
  64.                         }
  65.                     } else {
  66.                         print("Cant add input")
  67.                     }
  68.                 }
  69.                 catch{
  70.                     print("exception!");
  71.                 }
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement