daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest Mar 20th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2. import AVFoundation
  3. import Foundation
  4.  
  5. class ViewController: UIViewController {
  6.  
  7. @IBOutlet weak var navigationBar: UINavigationBar!
  8. @IBOutlet weak var imgOverlay: UIImageView!
  9. @IBOutlet weak var btnCapture: UIButton!
  10.  
  11. @IBOutlet weak var shapeLayer: UIView!
  12.  
  13. let captureSession = AVCaptureSession()
  14. let stillImageOutput = AVCaptureStillImageOutput()
  15. var previewLayer : AVCaptureVideoPreviewLayer?
  16.  
  17. //var shapeLayer : CALayer?
  18.  
  19. // If we find a device we'll store it here for later use
  20. var captureDevice : AVCaptureDevice?
  21.  
  22. override func viewDidLoad() {
  23.     super.viewDidLoad()
  24.     // Do any additional setup after loading the view, typically from a nib.
  25.     //=======================
  26.  
  27.     let midX = self.view.bounds.midX
  28.     let midY = self.view.bounds.midY
  29.  
  30.     let circlePath = UIBezierPath(arcCenter: CGPoint(x: midX,y: midY), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
  31.  
  32.     let shapeLayer = CAShapeLayer()
  33.  
  34.     shapeLayer.path = circlePath.cgPath
  35.     //change the fill color
  36.     shapeLayer.fillColor = UIColor.clear.cgColor
  37.     //you can change the stroke color
  38.     shapeLayer.strokeColor = UIColor.blue.cgColor
  39.     //you can change the line width
  40.     shapeLayer.lineWidth = 2.5
  41.  
  42.     view.layer.addSublayer(shapeLayer)
  43.     print("Shape layer drawn")
  44.     //=====================
  45.     captureSession.sessionPreset = AVCaptureSessionPresetHigh
  46.  
  47.     if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] {
  48.         // Loop through all the capture devices on this phone
  49.         for device in devices {
  50.             // Make sure this particular device supports video
  51.             if (device.hasMediaType(AVMediaTypeVideo)) {
  52.                 // Finally check the position and confirm we've got the back camera
  53.                 if(device.position == AVCaptureDevicePosition.back) {
  54.                     captureDevice = device
  55.                     if captureDevice != nil {
  56.                         print("Capture device found")
  57.                         beginSession()
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.     }
  63. }
  64.  
  65. @IBAction func actionCameraCapture(_ sender: AnyObject) {
  66.  
  67.     print("Camera button pressed")
  68.     saveToCamera()
  69. }
  70.  
  71. func beginSession() {
  72.  
  73.     do {
  74.         try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
  75.         stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
  76.  
  77.         if captureSession.canAddOutput(stillImageOutput) {
  78.             captureSession.addOutput(stillImageOutput)
  79.         }
  80.  
  81.     }
  82.     catch {
  83.         print("error: (error.localizedDescription)")
  84.     }
  85.  
  86.     guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else {
  87.         print("no preview layer")
  88.         return
  89.     }
  90.     // this is what displays the camera view. But - it's on TOP of the drawn view, and under the overview. ??
  91.     self.view.layer.addSublayer(previewLayer)
  92.     previewLayer.frame = self.view.layer.frame
  93.  
  94.  
  95.  
  96.     captureSession.startRunning()
  97.     print("Capture session running")
  98.  
  99.  
  100.     self.view.addSubview(navigationBar)
  101.     self.view.addSubview(imgOverlay)
  102.     self.view.addSubview(btnCapture)
  103.         }
  104.  
  105. func saveToCamera() {
  106.  
  107.     if let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) {
  108.           stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (CMSampleBuffer, Error) in
  109.  
  110.             if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(CMSampleBuffer) {
  111.                 if let cameraImage = UIImage(data: imageData) {
  112.  
  113.  
  114.                    UIImageWriteToSavedPhotosAlbum(cameraImage, nil, nil, nil)
  115.  
  116.                 }
  117.             }
  118.         })
  119.     }
  120. }
  121.  
  122.  
  123.  
  124. override func didReceiveMemoryWarning() {
  125.     super.didReceiveMemoryWarning()
  126.     // Dispose of any resources that can be recreated.
  127. }
  128.  
  129. }
  130.      
  131. @IBOutlet weak var shapeLayer: UIView!
  132.      
  133. let shapeLayer = CAShapeLayer()
  134.      
  135. let midX = self.view.bounds.midX
  136.     let midY = self.view.bounds.midY
  137.  
  138.     let circlePath = UIBezierPath(arcCenter: CGPoint(x: midX,y: midY), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
  139.  
  140.     let shapeLayerPath = CAShapeLayer()
  141.  
  142.     shapeLayerPath.path = circlePath.cgPath
  143.     //change the fill color
  144.     shapeLayerPath.fillColor = UIColor.clear.cgColor
  145.     //you can change the stroke color
  146.     shapeLayerPath.strokeColor = UIColor.blue.cgColor
  147.     //you can change the line width
  148.     shapeLayerPath.lineWidth = 2.5
  149.  
  150.     // add the blue-circle layer to the shapeLayer ImageView
  151.     shapeLayer.layer.addSublayer(shapeLayerPath)
  152.  
  153.     print("Shape layer drawn")
  154.     //=====================
  155.      
  156. self.view.addSubview(navigationBar)
  157.     self.view.addSubview(imgOverlay)
  158.     self.view.addSubview(btnCapture)
  159.  
  160.     // shapeLayer ImageView is already a subview created in IB
  161.     // but this will bring it to the front
  162.     self.view.addSubview(shapeLayer)
  163.  
  164.     // note: since these elements are added as @IBOutlet in IB,
  165.     // these could be:
  166.  
  167.     // self.view.bringSubview(toFront: navigationBar)
  168.     // self.view.bringSubview(toFront: imgOverlay)
  169.     // self.view.bringSubview(toFront: btnCapture)
  170.     // self.view.bringSubview(toFront: shapeLayer)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top