-
Notifications
You must be signed in to change notification settings - Fork 30
/
ViewController.swift
executable file
·145 lines (110 loc) · 4.42 KB
/
ViewController.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
//
// ViewController.swift
// StyleArts
//
// Created by Levin on 14/6/2017.
// Copyright © 2017 Levin . All rights reserved.
//
import UIKit
import CoreML
class ViewController: UIViewController, UINavigationControllerDelegate,UICollectionViewDelegate,UICollectionViewDataSource {
//MARK:- IBOutlets
@IBOutlet weak var CollectionView: UICollectionView!
@IBOutlet weak var imageView: UIImageView!
//MARK:- Properties
var originalImage:UIImage?
//MARK:- ViewController life cycles
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.imageView.layer.cornerRadius = 4
self.imageView.layer.borderWidth = 1.0
self.imageView.layer.borderColor = UIColor.black.cgColor
}
override func viewWillAppear(_ animated: Bool) {
self.originalImage = self.imageView.image
CollectionView.delegate = self
CollectionView.dataSource = self
}
//MARK:- Memory Management
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK:- IBActions
@IBAction func camera(_ sender: Any) {
if !UIImagePickerController.isSourceTypeAvailable(.camera) {
return
}
let cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.sourceType = .camera
cameraPicker.allowsEditing = false
present(cameraPicker, animated: true)
}
@IBAction func openLibrary(_ sender: Any) {
let picker = UIImagePickerController()
picker.allowsEditing = false
picker.delegate = self
picker.sourceType = .photoLibrary
present(picker, animated: true)
}
//MARK:- CollectionView datasource and delegates
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:FilterCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "filter", for: indexPath) as! FilterCollectionViewCell
switch indexPath.item {
case 0:
cell.lbl.text = "Mosaic"
cell.imageView.image = #imageLiteral(resourceName: "mosaicImg")
case 1:
cell.lbl.text = "Scream"
cell.imageView.image = #imageLiteral(resourceName: "screamImg")
case 2:
cell.lbl.text = "Muse"
cell.imageView.image = #imageLiteral(resourceName: "museImg")
case 3:
cell.lbl.text = "Udnie"
cell.imageView.image = #imageLiteral(resourceName: "Udanie")
case 4:
cell.lbl.text = "Candy"
cell.imageView.image = #imageLiteral(resourceName: "candy")
case 5:
cell.lbl.text = "Feathers"
cell.imageView.image = #imageLiteral(resourceName: "Feathers")
default:
cell.lbl.text = ""
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
DispatchQueue.main.async {
self.view.showHud(message: "Loading")
}
if self.imageView.image != nil{
StyleArt.shared.process(image: self.originalImage!, style: ArtStyle(rawValue: indexPath.row)!, compeletion: { (result) in
if let image = result{
self.imageView.image = image
}
})
}
DispatchQueue.main.async {
self.view.hideHud()
}
}
}
extension ViewController: UIImagePickerControllerDelegate {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true)
guard let image = info["UIImagePickerControllerOriginalImage"] as? UIImage else {
return
}
originalImage = image
imageView.image = image
}
}