回答編集履歴
1
回答の追記
answer
CHANGED
@@ -1,16 +1,176 @@
|
|
1
|
-
|
1
|
+
##追記
|
2
|
-
@IBAction func add(_ sender: Any) {
|
3
2
|
|
4
|
-
if UserDefaults.standard.object(forKey: "douga") != nil{
|
5
|
-
|
3
|
+
http://www.atotok.com/labo/ios_dev/20111121021151.html
|
6
|
-
|
4
|
+
この記事が参考になるかと思います。
|
5
|
+
UIImageの配列をUserDefaultsに保存していたためエラーが発生していました。
|
6
|
+
UIImageを保存する場合は、一旦Data型に変換する必要があります。なので、一旦uploadしたimageをdataに変換し、そのdataを配列に入れました。参考になれば幸いです
|
7
7
|
|
8
|
-
|
8
|
+
一旦そのまま修正箇所のコードを貼りますね
|
9
9
|
|
10
|
+
`AddViewController.swift`
|
11
|
+
変えた点は
|
12
|
+
①インスタンス変数に、`newArray: [Data]`を持たせる
|
13
|
+
②addメソッド内で、UIImageの配列をuserDefaultsに入れるのではなく、[Data]の配列を入れる
|
14
|
+
```swift
|
15
|
+
//
|
16
|
+
// AddViewController.swift
|
17
|
+
// Swift4TodoApp1
|
18
|
+
//
|
10
|
-
|
19
|
+
// Created by 服部 光男 on 2017/12/25.
|
11
|
-
|
20
|
+
// Copyright © 2017年 Hattori. All rights reserved.
|
21
|
+
//
|
12
22
|
|
23
|
+
import UIKit
|
24
|
+
|
25
|
+
class AddViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
|
26
|
+
|
27
|
+
@IBOutlet var imageView: UIImageView!
|
28
|
+
|
29
|
+
var array = [UIImage]()
|
30
|
+
|
31
|
+
//追加したやつ
|
32
|
+
var newArray = [Data]()
|
33
|
+
|
34
|
+
override func viewDidLoad() {
|
35
|
+
super.viewDidLoad()
|
36
|
+
imageView.image = UIImage(named: "default.png")
|
37
|
+
|
38
|
+
}
|
39
|
+
|
40
|
+
override func didReceiveMemoryWarning() {
|
41
|
+
super.didReceiveMemoryWarning()
|
42
|
+
// Dispose of any resources that can be recreated.
|
43
|
+
}
|
44
|
+
|
45
|
+
|
46
|
+
@IBAction func sentaku(_ sender: Any) {
|
47
|
+
// if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
|
48
|
+
// 写真を選ぶビュー
|
49
|
+
let pickerView = UIImagePickerController()
|
50
|
+
// 写真の選択元
|
51
|
+
pickerView.sourceType = .photoLibrary
|
52
|
+
pickerView.delegate = self
|
53
|
+
self.present(pickerView, animated: true)
|
54
|
+
// }
|
55
|
+
}
|
56
|
+
|
57
|
+
// 写真を選んだ後の処理
|
58
|
+
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
|
59
|
+
// 選択した写真を取得
|
60
|
+
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
|
61
|
+
// ビューに表示
|
62
|
+
self.imageView.image = image
|
63
|
+
// ビューを閉じる
|
64
|
+
self.dismiss(animated: true)
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
//addメソッド内を結構修正しました
|
69
|
+
@IBAction func add(_ sender: Any) {
|
70
|
+
if UserDefaults.standard.object(forKey: "douga") != nil {
|
71
|
+
// array = UserDefaults.standard.object(forKey: "douga") as! [UIImage]
|
72
|
+
newArray = UserDefaults.standard.object(forKey: "douga") as! [Data]
|
73
|
+
}
|
74
|
+
// array.append(imageView.image!)
|
75
|
+
guard let image = imageView.image else {
|
76
|
+
return
|
77
|
+
}
|
78
|
+
let data = UIImagePNGRepresentation(image)
|
79
|
+
if let dt = data {
|
80
|
+
newArray.append(dt)
|
81
|
+
UserDefaults.standard.set(newArray, forKey: "douga")
|
82
|
+
self.navigationController?.popViewController(animated: true)
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
86
|
+
|
87
|
+
/*
|
88
|
+
// MARK: - Navigation
|
89
|
+
|
90
|
+
// In a storyboard-based application, you will often want to do a little preparation before navigation
|
91
|
+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
92
|
+
// Get the new view controller using segue.destinationViewController.
|
93
|
+
// Pass the selected object to the new view controller.
|
94
|
+
}
|
95
|
+
*/
|
96
|
+
|
13
97
|
}
|
98
|
+
|
14
99
|
```
|
15
100
|
|
101
|
+
`ViewController.swift`
|
102
|
+
変えた点は
|
103
|
+
①インスタンス変数に、`newResultArray: [Data]`を持たせる
|
104
|
+
②viewWillAppearメソッド内で、UIImageの配列をuserDefaultsに入れるのではなく、[Data]の配列を入れる
|
105
|
+
|
106
|
+
```swift
|
107
|
+
//
|
108
|
+
// ViewController.swift
|
109
|
+
// Swift4TodoApp1
|
110
|
+
//
|
111
|
+
// Created by 服部 光男 on 2017/12/25.
|
112
|
+
// Copyright © 2017年 Hattori. All rights reserved.
|
113
|
+
//
|
114
|
+
|
115
|
+
import UIKit
|
116
|
+
|
117
|
+
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
118
|
+
|
119
|
+
@IBOutlet var tableView: UITableView!
|
120
|
+
|
121
|
+
var resultArray = [UIImage]()
|
122
|
+
|
123
|
+
//追加したやつ
|
124
|
+
var newResultArray: [Data]!
|
125
|
+
|
126
|
+
override func viewDidLoad() {
|
127
|
+
super.viewDidLoad()
|
128
|
+
|
129
|
+
tableView.delegate = self
|
130
|
+
tableView.dataSource = self
|
131
|
+
|
132
|
+
}
|
133
|
+
|
134
|
+
//中身修正
|
135
|
+
override func viewWillAppear(_ animated: Bool) {
|
136
|
+
super.viewWillAppear(animated)
|
137
|
+
if UserDefaults.standard.object(forKey: "douga") != nil {
|
138
|
+
newResultArray = UserDefaults.standard.object(forKey: "douga") as! [Data]
|
139
|
+
resultArray.removeAll()
|
140
|
+
for d in newResultArray {
|
141
|
+
let image = UIImage(data: d)
|
142
|
+
if let i = image {
|
143
|
+
resultArray.append(i)
|
144
|
+
}
|
145
|
+
}
|
146
|
+
}
|
147
|
+
tableView.reloadData()
|
148
|
+
}
|
149
|
+
|
150
|
+
|
151
|
+
func numberOfSections(in tableView: UITableView) -> Int {
|
152
|
+
return 1
|
153
|
+
|
154
|
+
}
|
155
|
+
|
156
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
157
|
+
return resultArray.count
|
158
|
+
}
|
159
|
+
|
16
|
-
|
160
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
161
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:indexPath)
|
162
|
+
cell.imageView?.image = resultArray[indexPath.row]
|
163
|
+
return cell
|
164
|
+
}
|
165
|
+
|
166
|
+
|
167
|
+
override func didReceiveMemoryWarning() {
|
168
|
+
super.didReceiveMemoryWarning()
|
169
|
+
// Dispose of any resources that can be recreated.
|
170
|
+
}
|
171
|
+
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
```
|