Guest User

Untitled

a guest
Jun 30th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. import UIKit
  2. import Firebase
  3. import AVFoundation
  4.  
  5. class CameraViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  6.  
  7. var captureSession : AVCaptureSession?
  8. var stillImageOutput : AVCaptureStillImageOutput?
  9. var previewLayer : AVCaptureVideoPreviewLayer?
  10. @IBOutlet var cameraView: UIView!
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13.  
  14. // Do any additional setup after loading the view.
  15. }
  16.  
  17. override func didReceiveMemoryWarning() {
  18. super.didReceiveMemoryWarning()
  19. // Dispose of any resources that can be recreated.
  20. }
  21.  
  22. override func viewDidAppear(animated: Bool) {
  23. super.viewDidAppear(animated)
  24. previewLayer?.frame = cameraView.bounds
  25. }
  26.  
  27. override func viewWillAppear(animated: Bool) {
  28. super.viewWillAppear(animated)
  29.  
  30. captureSession = AVCaptureSession()
  31. captureSession?.sessionPreset = AVCaptureSessionPresetPhoto //AVCaptureSessionPreset1920x1080
  32.  
  33. let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
  34.  
  35. var error : NSError?
  36. var input: AVCaptureDeviceInput!
  37. do {
  38. input = try AVCaptureDeviceInput(device: backCamera)
  39. } catch let error1 as NSError {
  40. error = error1
  41. input = nil
  42. }
  43.  
  44. if (error == nil && captureSession?.canAddInput(input) != nil){
  45.  
  46. captureSession?.addInput(input)
  47.  
  48. stillImageOutput = AVCaptureStillImageOutput()
  49. stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
  50.  
  51. if (captureSession?.canAddOutput(stillImageOutput) != nil){
  52. captureSession?.addOutput(stillImageOutput)
  53.  
  54. previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
  55. previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
  56. previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
  57. cameraView.layer.addSublayer(previewLayer!)
  58. captureSession?.startRunning()
  59.  
  60. }
  61.  
  62.  
  63. }
  64.  
  65.  
  66. }
  67.  
  68. @IBOutlet var tempImageView: UIImageView!
  69.  
  70.  
  71. func didPressTakePhoto(){
  72.  
  73. if let videoConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo){
  74. videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
  75. stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
  76. (sampleBuffer, error) in
  77.  
  78. if sampleBuffer != nil {
  79.  
  80.  
  81. var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
  82. var dataProvider = CGDataProviderCreateWithCFData(imageData)
  83. var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)
  84.  
  85. var image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
  86.  
  87. self.tempImageView.image = image
  88. self.tempImageView.hidden = false
  89.  
  90. }
  91.  
  92.  
  93. })
  94. }
  95.  
  96.  
  97. }
  98.  
  99.  
  100. var didTakePhoto = Bool()
  101.  
  102. func didPressTakeAnother(){
  103. if didTakePhoto == true{
  104. tempImageView.hidden = true
  105. didTakePhoto = false
  106.  
  107. }
  108. else{
  109. captureSession?.startRunning()
  110. didTakePhoto = true
  111. didPressTakePhoto()
  112.  
  113. }
  114.  
  115. }
  116.  
  117. override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
  118. didPressTakeAnother()
  119. }
  120. }
Add Comment
Please, Sign In to add comment