2018-08-16 11:18:27 +02:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
@IBOutlet weak var imageButton: UIButton!
|
2018-08-16 11:18:27 +02:00
|
|
|
|
|
|
|
@IBOutlet weak var cropView: CropView!
|
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
@IBOutlet weak var cancelButton: UIButton!
|
2018-08-16 11:18:27 +02:00
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
@IBOutlet weak var bottomBar: UIView!
|
|
|
|
|
2018-08-16 11:18:27 +02:00
|
|
|
@IBOutlet weak var cameraView: CameraView! {
|
|
|
|
didSet {
|
|
|
|
cameraView.configure()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var delegate: CameraControllerDelegate?
|
2019-03-15 13:19:19 +01:00
|
|
|
|
|
|
|
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
|
|
|
|
return .portrait
|
|
|
|
}
|
|
|
|
|
|
|
|
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
|
|
|
|
return .portrait
|
|
|
|
}
|
|
|
|
|
|
|
|
override var shouldAutorotate: Bool {
|
|
|
|
return false
|
|
|
|
}
|
2018-08-16 11:18:27 +02:00
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction func backButtonPressed() {
|
2019-03-15 13:19:19 +01:00
|
|
|
self.giveFeedback(.medium)
|
2018-08-16 11:18:27 +02:00
|
|
|
delegate?.didCancel()
|
|
|
|
self.dismiss(animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func imageButtonPressed() {
|
2019-03-15 13:19:19 +01:00
|
|
|
self.giveFeedback(.medium)
|
2018-08-16 11:18:27 +02:00
|
|
|
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() {
|
2020-05-16 11:21:55 +02:00
|
|
|
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)
|
2018-08-16 11:18:27 +02:00
|
|
|
}
|
2019-03-15 13:19:19 +01:00
|
|
|
|
|
|
|
private func giveFeedback(_ style: UIImpactFeedbackGenerator.FeedbackStyle) {
|
|
|
|
let generator = UIImpactFeedbackGenerator(style: style)
|
|
|
|
generator.impactOccurred()
|
|
|
|
}
|
2018-08-16 11:18:27 +02:00
|
|
|
|
|
|
|
// 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.",
|
2020-05-16 11:21:55 +02:00
|
|
|
preferredStyle: .alert)//,
|
|
|
|
//blurStyle: .dark)
|
2018-08-16 11:18:27 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2019-03-15 13:19:19 +01:00
|
|
|
|
|
|
|
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
|
|
|
cameraView.didReceiveTouch(touches.first!)
|
|
|
|
}
|
2018-08-16 11:18:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension CameraController: PhotoCaptureHandlerDelegate {
|
|
|
|
|
|
|
|
func didCapture(_ image: UIImage?) {
|
2020-05-16 11:21:55 +02:00
|
|
|
log("Image captured")
|
2018-08-16 11:18:27 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
let scaled = masked.resize(to: Cap.imageSize)
|
2018-08-16 11:18:27 +02:00
|
|
|
|
|
|
|
delegate!.didCapture(image: scaled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
extension CameraController: Logger { }
|