) -> State {
54 | switch result {
55 | case .success(let image):
56 | return .success(image)
57 | case .failure(let error):
58 | return .failure(error)
59 | }
60 | }
61 |
62 | private func render(_ state: State) {
63 | switch state {
64 | case .idle:
65 | activityIndicator.isHidden = true
66 | imageView.image = nil
67 | case .loading:
68 | activityIndicator.isHidden = false
69 | imageView.image = nil
70 | case .failure(let error):
71 | activityIndicator.isHidden = true
72 | imageView.image = nil
73 | showErrorAlert(error)
74 | case .success(let image):
75 | activityIndicator.isHidden = true
76 | imageView.image = image
77 | }
78 | }
79 |
80 | private func showErrorAlert(_ error: Error) {
81 | let alert = UIAlertController(title: "Message", message: "Failed to colorize the image!", preferredStyle: .alert)
82 | alert.addAction(UIAlertAction(title: "Ok", style: .cancel))
83 | UIViewController.topmostViewContoller.present(alert, animated: true)
84 | }
85 |
86 | private func selectImage(_ completion: @escaping ImageProvider.Completion) {
87 | let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
88 | alert.addAction(UIAlertAction(title: "Photos", style: .default) { _ in
89 | self.picker.image(with: completion)
90 | })
91 | alert.addAction(UIAlertAction(title: "Camera", style: .default) { _ in
92 | self.scanner.image(with: completion)
93 | })
94 | alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
95 | UIViewController.topmostViewContoller.present(alert, animated: true)
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Grayscale image colorizer
2 |
3 | This is the demo project of the article written in [my blog](https://onswiftwings.com/posts/image-colorization-coreml). The application colorizes grayscale images using CoreML, Vision and CoreImage frameworks.
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/screenshots/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sgl0v/ImageColorizer/9d34b28e473ac2d7438cbece0979d00262c0d447/screenshots/demo.gif
--------------------------------------------------------------------------------