Caps-iOS/CapCollector/Capture/CameraController.swift
2020-05-16 11:21:55 +02:00

172 lines
5.1 KiB
Swift

//
// CameraController.swift
// CapFinder
//
// Created by User on 22.02.18.
// Copyright © 2018 User. All rights reserved.
//
import UIKit
protocol CameraControllerDelegate {
func didCapture(image: UIImage)
func didCancel()
}
class CameraController: UIViewController {
// MARK: Outlets
@IBOutlet weak var imageButton: UIButton!
@IBOutlet weak var cropView: CropView!
@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var bottomBar: UIView!
@IBOutlet weak var cameraView: CameraView! {
didSet {
cameraView.configure()
}
}
var delegate: CameraControllerDelegate?
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
override var shouldAutorotate: Bool {
return false
}
// MARK: Actions
@IBAction func backButtonPressed() {
self.giveFeedback(.medium)
delegate?.didCancel()
self.dismiss(animated: true)
}
@IBAction func imageButtonPressed() {
self.giveFeedback(.medium)
imageButton.isEnabled = false
cameraView.capture()
}
// MARK: Life cycle
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
cameraView.delegate = self
imageButton.imageView?.image = UIImage.templateImage(named: "camera")
setTintColor()
cameraView.launch { success, error in
guard let err = error else {
return
}
switch err {
case "No camera access": self.showNoCameraAccessAlert()
case "Camera error": self.showAlert("Unable to capture media")
default: self.showAlert("Error in camera setup")
}
}
self.imageButton.isEnabled = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
cameraView.complete()
}
private func setTintColor() {
let blur = UIBlurEffect(style: .systemThinMaterial)
let a = UIVisualEffectView(effect: blur)
a.translatesAutoresizingMaskIntoConstraints = false
bottomBar.backgroundColor = nil
bottomBar.insertSubview(a, at: 0)
//bottomBar.addSubview(a)
let t = bottomBar.topAnchor.constraint(equalTo: a.topAnchor)
let b = bottomBar.bottomAnchor.constraint(equalTo: a.bottomAnchor)
let l = bottomBar.leadingAnchor.constraint(equalTo: a.leadingAnchor)
let r = bottomBar.trailingAnchor.constraint(equalTo: a.trailingAnchor)
bottomBar.addConstraints([t,b,l,r])
//let tint = AppDelegate.tintColor
//cropView.lineColor = tint
//imageButton.borderColor = tint
imageButton.imageView?.tintColor = .systemBlue
//cancelButton.borderColor = tint
//cancelButton.set(template: "cancel", with: tint)
}
private func giveFeedback(_ style: UIImpactFeedbackGenerator.FeedbackStyle) {
let generator = UIImpactFeedbackGenerator(style: style)
generator.impactOccurred()
}
// MARK: Alerts
private func showNoCameraAccessAlert() {
let alert = UIAlertController(title: "Unable to access the Camera",
message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.",
preferredStyle: .alert)//,
//blurStyle: .dark)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(okAction)
let settingsAction = UIAlertAction(title: "Settings", style: .default, handler: { _ in
// Take the user to Settings app to possibly change permission.
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return }
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: nil)
}
})
alert.addAction(settingsAction)
self.present(alert, animated: true, completion: nil)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
cameraView.didReceiveTouch(touches.first!)
}
}
extension CameraController: PhotoCaptureHandlerDelegate {
func didCapture(_ image: UIImage?) {
log("Image captured")
let factor = CGFloat(cropView.relativeSize)
self.dismiss(animated: true)
guard let img = image else {
self.error("No image captured")
return
}
guard delegate != nil else {
self.error("No delegate")
return
}
guard let masked = img.crop(factor: factor).circleMasked else {
self.error("Could not mask image")
return
}
let scaled = masked.resize(to: Cap.imageSize)
delegate!.didCapture(image: scaled)
}
}
extension CameraController: Logger { }