forked from WeTransfer/WeScan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeViewController.swift
158 lines (124 loc) · 5.88 KB
/
HomeViewController.swift
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// ViewController.swift
// WeScanSampleProject
//
// Created by Boris Emorine on 2/8/18.
// Copyright © 2018 WeTransfer. All rights reserved.
//
import UIKit
import WeScan
final class HomeViewController: UIViewController {
lazy private var logoImageView: UIImageView = {
let image = UIImage(named: "WeScanLogo")
let imageView = UIImageView(image: image)
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
lazy private var logoLabel: UILabel = {
let label = UILabel()
label.text = "WeScan"
label.font = UIFont.systemFont(ofSize: 25.0, weight: .bold)
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy private var scanButton: UIButton = {
let button = UIButton(type: .custom)
button.titleLabel?.font = UIFont.preferredFont(forTextStyle: .headline)
button.setTitle("Scan Item", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(scanOrSelectImage(_:)), for: .touchUpInside)
button.backgroundColor = UIColor(red: 64.0 / 255.0, green: 159 / 255.0, blue: 255 / 255.0, alpha: 1.0)
button.layer.cornerRadius = 10.0
return button
}()
// MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
setupConstraints()
}
// MARK: - Setups
private func setupViews() {
view.addSubview(logoImageView)
view.addSubview(logoLabel)
view.addSubview(scanButton)
}
private func setupConstraints() {
let logoImageViewConstraints = [
logoImageView.widthAnchor.constraint(equalToConstant: 150.0),
logoImageView.heightAnchor.constraint(equalToConstant: 150.0),
logoImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
NSLayoutConstraint(item: logoImageView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 0.75, constant: 0.0)
]
let logoLabelConstraints = [
logoLabel.topAnchor.constraint(equalTo: logoImageView.bottomAnchor, constant: 20.0),
logoLabel.centerXAnchor.constraint(equalTo: logoImageView.centerXAnchor)
]
NSLayoutConstraint.activate(logoLabelConstraints + logoImageViewConstraints)
if #available(iOS 11.0, *) {
let scanButtonConstraints = [
scanButton.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 16),
scanButton.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: -16),
scanButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -16),
scanButton.heightAnchor.constraint(equalToConstant: 55)
]
NSLayoutConstraint.activate(scanButtonConstraints)
} else {
let scanButtonConstraints = [
scanButton.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 16),
scanButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -16),
scanButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16),
scanButton.heightAnchor.constraint(equalToConstant: 55)
]
NSLayoutConstraint.activate(scanButtonConstraints)
}
}
// MARK: - Actions
@objc func scanOrSelectImage(_ sender: UIButton) {
let actionSheet = UIAlertController(title: "Would you like to scan an image or select one from your photo library?", message: nil, preferredStyle: .actionSheet)
let scanAction = UIAlertAction(title: "Scan", style: .default) { (_) in
self.scanImage()
}
let selectAction = UIAlertAction(title: "Select", style: .default) { (_) in
self.selectImage()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
actionSheet.addAction(scanAction)
actionSheet.addAction(selectAction)
actionSheet.addAction(cancelAction)
present(actionSheet, animated: true)
}
func scanImage() {
let scannerViewController = ImageScannerController(delegate: self)
present(scannerViewController, animated: true)
}
func selectImage() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true)
}
}
extension HomeViewController: ImageScannerControllerDelegate {
func imageScannerController(_ scanner: ImageScannerController, didFailWithError error: Error) {
print(error)
}
func imageScannerController(_ scanner: ImageScannerController, didFinishScanningWithResults results: ImageScannerResults) {
scanner.dismiss(animated: true, completion: nil)
}
func imageScannerControllerDidCancel(_ scanner: ImageScannerController) {
scanner.dismiss(animated: true, completion: nil)
}
}
extension HomeViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true)
guard let image = info[.originalImage] as? UIImage else { return }
let scannerViewController = ImageScannerController(image: image, delegate: self)
present(scannerViewController, animated: true)
}
}