Blame view

App/camera/ALCameraLib/Utilities/CameraShot.swift 1.05 KB
1341bf603   Trịnh Văn Quân   version 1.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  //
  //  CameraShot.swift
  //  ALCameraViewController
  //
  //  Created by Alex Littlejohn on 2015/06/17.
  //  Copyright (c) 2015 zero. All rights reserved.
  //
  
  import UIKit
  import AVFoundation
  
  public typealias CameraShotCompletion = (UIImage?) -> Void
  
  public func takePhoto(_ stillImageOutput: AVCaptureStillImageOutput, videoOrientation: AVCaptureVideoOrientation, cropSize: CGSize, completion: @escaping CameraShotCompletion) {
      
      guard let videoConnection: AVCaptureConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) else {
          completion(nil)
          return
      }
      
      videoConnection.videoOrientation = videoOrientation
      
      stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { buffer, error in
          
          guard let buffer = buffer,
              let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer),
              let image = UIImage(data: imageData) else {
              completion(nil)
              return
          }
          
          completion(image)
      })
  }