我想实现相机动作,例如拍照(并将其传递给下一个控制器),开/关闪光灯,更改相机(后/前),但是我以前从未这样做过。我实现了全视图相机控制器,并以编程方式创建了所有UI。 我该怎么做?我在这里找到了一些答案,但它仅适用于Swift 2,我使用的是第4版。
我的CameraViewController
import UIKit
import AVFoundation
class CameraViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var captureSession = AVCaptureSession()
var backCamera: AVCaptureDevice?
var frontCamera: AVCaptureDevice?
var currentCamera: AVCaptureDevice?
var photoOutput: AVCapturePhotoOutput?
var cameraPreviewLayer: AVCaptureVideoPreviewLayer?
var image: UIImage?
var style:UIStatusBarStyle = .default
var imagePicker: UIImagePickerController!
lazy var UI = CameraUI(view: self.view)
override func viewDidLoad() {
super.viewDidLoad()
addUIElements()
setupCaptureSession()
setupDevice()
setupInputOutput()
setupPreviewLayer()
startRunningCaptureSession()
addActions()
}
func addActions(){
UI.changeCamera.addTarget(self, action: #selector(actionChange), for: .touchUpInside)
UI.changeFlash.addTarget(self, action: #selector(actionFlash), for: .touchUpInside)
UI.makePhoto.addTarget(self, action: #selector(actionPhoto), for: .touchUpInside)
}
@objc func actionChange(){
let settings = AVCapturePhotoSettings()
photoOutput?.capturePhoto(with: settings, delegate: self)
}
@objc func actionFlash(){
let settings = AVCapturePhotoSettings()
photoOutput?.capturePhoto(with: settings, delegate: self)
}
@objc func actionPhoto(){
let settings = AVCapturePhotoSettings()
photoOutput?.capturePhoto(with: settings, delegate: self)
let addPhotoVC = AddPhotoFromCameraViewController()
self.navigationController?.pushViewController(addPhotoVC, animated: true)
}
private func addUIElements(){
UI.addElementsToSuperView()
view.layoutIfNeeded()
}
func setupCaptureSession(){
captureSession.sessionPreset = AVCaptureSession.Preset.photo
}
func setupDevice(){
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
let devices = deviceDiscoverySession.devices
for device in devices{
if device.position == AVCaptureDevice.Position.back{
backCamera = device
}else if device.position == AVCaptureDevice.Position.front{
frontCamera = device
}
}
currentCamera = backCamera
}
func setupInputOutput(){
do{
let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
captureSession.addInput(captureDeviceInput)
photoOutput = AVCapturePhotoOutput()
photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
captureSession.addOutput(photoOutput!)
}catch{
print(error)
}
}
func setupPreviewLayer(){
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraPreviewLayer?.frame = self.view.frame
view.layer.insertSublayer(cameraPreviewLayer!, at: 0)
}
func startRunningCaptureSession(){
captureSession.startRunning()
}
override func viewWillAppear(_ animated: Bool) {
setupNavBar()
navigationController?.navigationBar.prefersLargeTitles = false
navigationController?.navigationItem.largeTitleDisplayMode = .automatic
hidenTabBar()
}
override func viewWillDisappear(_ animated: Bool) {
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationItem.largeTitleDisplayMode = .always
shownTabBar()
}
func setupNavBar(){
title = "Camera"
navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: PineappleColors.green.color]
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.65)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.65)
navigationController?.view.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.65)
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationController?.navigationBar.tintColor = PineappleColors.green.color
self.style = .lightContent
self.setNeedsStatusBarAppearanceUpdate()
}
}
扩展名
extension CameraViewController: AVCapturePhotoCaptureDelegate{
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let imageData = photo.fileDataRepresentation() {
print(imageData)
image = UIImage(data: imageData)
//perform segue
}
}
}