-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
150 lines (109 loc) · 5.88 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
146
147
148
149
//
// ViewController.swift
// Project13
//
// Created by Eren Erinanc on 28.01.2021.
//
import CoreImage
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var imageView: UIImageView!
@IBOutlet var intensity: UISlider!
@IBOutlet var radius: UISlider!
@IBOutlet var changeFilterButton: UIButton!
var currentImage: UIImage!
var context: CIContext!
var currentFilter: CIFilter!
override func viewDidLoad() {
super.viewDidLoad()
title = "Photo Edit"
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(importPicture))
context = CIContext()
currentFilter = CIFilter(name: "CISepiaTone")
changeFilterButton.setTitle("Change Filter", for: .normal)
changeFilterButton.setTitleColor(UIColor.systemBlue, for: .normal)
}
@objc func importPicture() {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
present(picker, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
dismiss(animated: true)
UIView.animate(withDuration: 1, delay: 0, options: [], animations: {
self.imageView.alpha = 0
self.currentImage = image
self.imageView.alpha = 1
})
let beginImage = CIImage(image: currentImage)
currentFilter.setValue(beginImage, forKey: kCIInputImageKey)
applyProcessing()
}
@IBAction func changeFilter(_ sender: UIButton) {
let ac = UIAlertController(title: "Choose filter", message: nil, preferredStyle: .actionSheet)
ac.addAction(UIAlertAction(title: "CIBumpDistortion", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIGaussianBlur", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIPixellate", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CISepiaTone", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CITwirlDistortion", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIUnsharpMask", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIVignette", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
if let popoverController = ac.popoverPresentationController {
popoverController.sourceView = sender
popoverController.sourceRect = sender.bounds
}
present(ac, animated: true)
}
func setFilter(action: UIAlertAction) {
guard currentImage != nil else { return }
guard let actionTitle = action.title else { return }
currentFilter = CIFilter(name: actionTitle)
let beginImage = CIImage(image: currentImage)
currentFilter.setValue(beginImage, forKey: kCIInputImageKey)
changeFilterButton.setTitle(currentFilter.name, for: .normal)
}
@IBAction func save(_ sender: Any) {
guard let image = imageView.image else {
let ac = UIAlertController(title: "No image found", message: "You can add an image tapping the right button on top of the screen.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
return }
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_image:didFinishSavingWithError:contextInfo:)), nil)
}
@IBAction func intensityChanged(_ sender: Any) {
applyProcessing()
}
@IBAction func radiusChanged(_ sender: Any) {
applyProcessing()
}
func applyProcessing() {
let inputKeys = currentFilter.inputKeys
if inputKeys.contains(kCIInputIntensityKey) { currentFilter.setValue(intensity.value, forKey: kCIInputIntensityKey) }
if inputKeys.contains(kCIInputRadiusKey) { currentFilter.setValue(radius.value * 200, forKey: kCIInputRadiusKey) }
if inputKeys.contains(kCIInputCenterKey) { currentFilter.setValue(CIVector(x: currentImage.size.width / 2, y: currentImage.size.height / 2), forKey: kCIInputCenterKey) }
if inputKeys.contains(kCIInputScaleKey) { currentFilter.setValue(intensity.value * 10, forKey: kCIInputScaleKey) }
guard let image = currentFilter.outputImage else {
let ac = UIAlertController(title: "No image found", message: "You can add an image tapping the right button on top of the screen.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
return }
if let cgimg = context.createCGImage(image, from: image.extent) {
let processedImage = UIImage(cgImage: cgimg)
imageView.image = processedImage
}
}
@objc func image(_image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
}