-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
151 lines (119 loc) · 5.85 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
150
//
// ViewController.swift
// InstaFilter
//
// Created by Julian Moorhouse on 08/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
import CoreImage
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var imageView: UIImageView!
@IBOutlet var intensity: UISlider!
@IBOutlet var radius: UISlider!
var currentImage: UIImage!
var context: CIContext!
var currentFilter: CIFilter!
override func viewDidLoad() {
super.viewDidLoad()
title = "InstaFilter"
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(importPicture))
imageView.alpha = 0.0
context = CIContext()
currentFilter = CIFilter(name: "CISepiaTone")
title = "InstaFiler - CISepiaTone"
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
dismiss(animated: true)
currentImage = image
imageView.image = image
imageView.alpha = 0.0
UIView.animate(withDuration: 1, delay: 0, options: [], animations: {
self.imageView.alpha = 1.0
}, completion: nil)
let beginImage = CIImage(image: currentImage)
currentFilter.setValue(beginImage, forKey: kCIInputImageKey)
applyProcessing()
}
@objc func importPicture() {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
present(picker, animated: true)
}
@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: "Canel", style: .cancel))
if let popoverController = ac.popoverPresentationController {
popoverController.sourceView = sender
popoverController.sourceRect = sender.bounds
}
present(ac, animated: true)
}
@IBAction func save(_ sender: Any) {
guard let image = imageView.image else {
let ac = UIAlertController(title: "Error", message: "Please select an image first!", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
return
}
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
@IBAction func intensityChanged(_ 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(kCIInputScaleKey) {
currentFilter.setValue(intensity.value * 10, forKey: kCIInputScaleKey)
}
if inputKeys.contains(kCIInputCenterKey) {
currentFilter.setValue(CIVector(x: currentImage.size.width / 2, y: currentImage.size.height / 2), forKey: kCIInputCenterKey)
}
guard let outputImage = currentFilter.outputImage else { return }
if let cgImage = context.createCGImage(outputImage, from: outputImage.extent) {
let processedImage = UIImage(cgImage: cgImage)
imageView.image = processedImage
}
}
func setFilter(action: UIAlertAction) {
guard currentFilter != nil else { return }
guard let actionTitle = action.title else { return }
guard currentImage != nil else {
let ac = UIAlertController(title: "Error", message: "Please select an image first!", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
return
}
title = "InstaFiler - \(actionTitle)"
currentFilter = CIFilter(name: actionTitle)
let beginImage = CIImage(image: currentImage)
currentFilter.setValue(beginImage, forKey: kCIInputImageKey)
applyProcessing()
}
@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)
}
}
}