-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
146 lines (108 loc) · 4.8 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
// CameraCaptions
//
// Created by Julian Moorhouse on 07/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
class ViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var photos = [Photo]()
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNewPhoto))
title = "Photos"
performSelector(inBackground: #selector(loadImages), with: nil)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return photos.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Photo", for: indexPath)
cell.textLabel?.text = photos[indexPath.row].caption
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
let photo = photos[indexPath.row]
vc.selectedImage = photo.imageFileName
vc.caption = photo.caption
save()
tableView.performSelector(onMainThread: #selector(UITableView.reloadData), with: nil, waitUntilDone: false)
navigationController?.pushViewController(vc, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
let imageName = UUID().uuidString
let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)
dismiss(animated: true)
let ac = UIAlertController(title: "Photo", message: "Please provide a name for this photo", preferredStyle: .alert)
ac.addTextField()
ac.addAction(UIAlertAction(title: "OK", style: .default) {
[weak self, weak ac] _ in
guard let newName = ac?.textFields?[0].text else { return }
if let jpegData = image.jpegData(compressionQuality: 0.8) {
try? jpegData.write(to: imagePath)
}
let photo = Photo(imageFileName: imageName, caption: newName)
self?.photos.append(photo)
self?.save()
self?.tableView.reloadData()
})
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(ac, animated: true)
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
@objc func loadImages() {
// let fm = FileManager.default
// let path = Bundle.main.resourcePath!
// let items = try! fm.contentsOfDirectory(atPath: path)
//
// for item in items {
// if item.hasPrefix("nssl") {
// // this is a picture to load!
// storms.append(Storm(pictureName: item, viewCount: 0))
// }
// }
//
load()
//storms.sort()
print(photos)
tableView.performSelector(onMainThread: #selector(UITableView.reloadData), with: nil, waitUntilDone: false)
}
@objc func addNewPhoto() {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera))
{
picker.sourceType = UIImagePickerController.SourceType.camera
}
present(picker, animated: true)
}
func save() {
let jsonEncoder = JSONEncoder()
if let savedDate = try? jsonEncoder.encode(photos) {
let defaults = UserDefaults.standard
defaults.set(savedDate, forKey: "photos")
} else {
print("Failed to save photos")
}
}
func load() {
let defaults = UserDefaults.standard
if let savedPhotos = defaults.object(forKey: "photos") as? Data {
let jsonDecoder = JSONDecoder()
do {
photos = try jsonDecoder.decode([Photo].self, from: savedPhotos)
} catch {
print("Failed to load photos.")
}
}
}
}