回答編集履歴

1

回答の追記

2018/01/15 00:20

投稿

komo_ta
komo_ta

スコア275

test CHANGED
@@ -1,31 +1,351 @@
1
+ ##追記
2
+
3
+
4
+
5
+ http://www.atotok.com/labo/ios_dev/20111121021151.html
6
+
7
+ この記事が参考になるかと思います。
8
+
9
+ UIImageの配列をUserDefaultsに保存していたためエラーが発生していました。
10
+
11
+ UIImageを保存する場合は、一旦Data型に変換する必要があります。なので、一旦uploadしたimageをdataに変換し、そのdataを配列に入れました。参考になれば幸いです
12
+
13
+
14
+
15
+ 一旦そのまま修正箇所のコードを貼りますね
16
+
17
+
18
+
19
+ `AddViewController.swift`
20
+
21
+ 変えた点は
22
+
23
+ ①インスタンス変数に、`newArray: [Data]`を持たせる
24
+
25
+ ②addメソッド内で、UIImageの配列をuserDefaultsに入れるのではなく、[Data]の配列を入れる
26
+
27
+ ```swift
28
+
29
+ //
30
+
31
+ // AddViewController.swift
32
+
33
+ // Swift4TodoApp1
34
+
35
+ //
36
+
37
+ // Created by 服部 光男 on 2017/12/25.
38
+
39
+ // Copyright © 2017年 Hattori. All rights reserved.
40
+
41
+ //
42
+
43
+
44
+
45
+ import UIKit
46
+
47
+
48
+
49
+ class AddViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
50
+
51
+
52
+
53
+ @IBOutlet var imageView: UIImageView!
54
+
55
+
56
+
57
+ var array = [UIImage]()
58
+
59
+
60
+
61
+ //追加したやつ
62
+
63
+ var newArray = [Data]()
64
+
65
+
66
+
67
+ override func viewDidLoad() {
68
+
69
+ super.viewDidLoad()
70
+
71
+ imageView.image = UIImage(named: "default.png")
72
+
73
+
74
+
75
+ }
76
+
77
+
78
+
79
+ override func didReceiveMemoryWarning() {
80
+
81
+ super.didReceiveMemoryWarning()
82
+
83
+ // Dispose of any resources that can be recreated.
84
+
85
+ }
86
+
87
+
88
+
89
+
90
+
91
+ @IBAction func sentaku(_ sender: Any) {
92
+
93
+ // if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
94
+
95
+ // 写真を選ぶビュー
96
+
97
+ let pickerView = UIImagePickerController()
98
+
99
+ // 写真の選択元
100
+
101
+ pickerView.sourceType = .photoLibrary
102
+
103
+ pickerView.delegate = self
104
+
105
+ self.present(pickerView, animated: true)
106
+
107
+ // }
108
+
109
+ }
110
+
111
+
112
+
113
+ // 写真を選んだ後の処理
114
+
115
+ func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
116
+
117
+ // 選択した写真を取得
118
+
119
+ let image = info[UIImagePickerControllerOriginalImage] as! UIImage
120
+
121
+ // ビューに表示
122
+
123
+ self.imageView.image = image
124
+
125
+ // ビューを閉じる
126
+
127
+ self.dismiss(animated: true)
128
+
129
+ }
130
+
131
+
132
+
133
+
134
+
135
+ //addメソッド内を結構修正しました
136
+
137
+ @IBAction func add(_ sender: Any) {
138
+
139
+ if UserDefaults.standard.object(forKey: "douga") != nil {
140
+
141
+ // array = UserDefaults.standard.object(forKey: "douga") as! [UIImage]
142
+
143
+ newArray = UserDefaults.standard.object(forKey: "douga") as! [Data]
144
+
145
+ }
146
+
147
+ // array.append(imageView.image!)
148
+
149
+ guard let image = imageView.image else {
150
+
151
+ return
152
+
153
+ }
154
+
155
+ let data = UIImagePNGRepresentation(image)
156
+
157
+ if let dt = data {
158
+
159
+ newArray.append(dt)
160
+
161
+ UserDefaults.standard.set(newArray, forKey: "douga")
162
+
163
+ self.navigationController?.popViewController(animated: true)
164
+
165
+ }
166
+
167
+ }
168
+
169
+
170
+
171
+
172
+
173
+ /*
174
+
175
+ // MARK: - Navigation
176
+
177
+
178
+
179
+ // In a storyboard-based application, you will often want to do a little preparation before navigation
180
+
181
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
182
+
183
+ // Get the new view controller using segue.destinationViewController.
184
+
185
+ // Pass the selected object to the new view controller.
186
+
187
+ }
188
+
189
+ */
190
+
191
+
192
+
193
+ }
194
+
195
+
196
+
1
197
  ```
2
198
 
199
+
200
+
201
+ `ViewController.swift`
202
+
203
+ 変えた点は
204
+
205
+ ①インスタンス変数に、`newResultArray: [Data]`を持たせる
206
+
207
+ ②viewWillAppearメソッド内で、UIImageの配列をuserDefaultsに入れるのではなく、[Data]の配列を入れる
208
+
209
+
210
+
211
+ ```swift
212
+
213
+ //
214
+
215
+ // ViewController.swift
216
+
217
+ // Swift4TodoApp1
218
+
219
+ //
220
+
221
+ // Created by 服部 光男 on 2017/12/25.
222
+
223
+ // Copyright © 2017年 Hattori. All rights reserved.
224
+
225
+ //
226
+
227
+
228
+
229
+ import UIKit
230
+
231
+
232
+
233
+ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
234
+
235
+
236
+
237
+ @IBOutlet var tableView: UITableView!
238
+
239
+
240
+
241
+ var resultArray = [UIImage]()
242
+
243
+
244
+
245
+ //追加したやつ
246
+
247
+ var newResultArray: [Data]!
248
+
249
+
250
+
3
- @IBAction func add(_ sender: Any) {
251
+ override func viewDidLoad() {
252
+
4
-
253
+ super.viewDidLoad()
254
+
255
+
256
+
5
-
257
+ tableView.delegate = self
258
+
6
-
259
+ tableView.dataSource = self
260
+
261
+
262
+
263
+ }
264
+
265
+
266
+
267
+ //中身修正
268
+
269
+ override func viewWillAppear(_ animated: Bool) {
270
+
271
+ super.viewWillAppear(animated)
272
+
7
- if UserDefaults.standard.object(forKey: "douga") != nil{
273
+ if UserDefaults.standard.object(forKey: "douga") != nil {
8
-
274
+
9
- array = UserDefaults.standard.object(forKey: "douga") as! [UIImage]
275
+ newResultArray = UserDefaults.standard.object(forKey: "douga") as! [Data]
276
+
277
+ resultArray.removeAll()
278
+
279
+ for d in newResultArray {
280
+
281
+ let image = UIImage(data: d)
282
+
283
+ if let i = image {
284
+
285
+ resultArray.append(i)
286
+
287
+ }
288
+
289
+ }
290
+
291
+ }
292
+
293
+ tableView.reloadData()
294
+
295
+ }
296
+
297
+
298
+
299
+
300
+
301
+ func numberOfSections(in tableView: UITableView) -> Int {
302
+
303
+ return 1
304
+
305
+
306
+
307
+ }
308
+
309
+
310
+
311
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
312
+
313
+ return resultArray.count
314
+
315
+ }
316
+
317
+
318
+
319
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
320
+
321
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:indexPath)
322
+
323
+ cell.imageView?.image = resultArray[indexPath.row]
324
+
325
+ return cell
326
+
327
+ }
328
+
329
+
330
+
331
+
332
+
333
+ override func didReceiveMemoryWarning() {
334
+
335
+ super.didReceiveMemoryWarning()
336
+
337
+ // Dispose of any resources that can be recreated.
338
+
339
+ }
340
+
341
+
342
+
343
+
10
344
 
11
345
  }
12
346
 
13
347
 
14
348
 
15
- array.append(imageView.image!)
349
+
16
-
17
-
18
-
19
- UserDefaults.standard.set(array, forKey: "douga")
20
-
21
- self.navigationController?.popViewController(animated: true)
22
-
23
-
24
-
25
- }
26
350
 
27
351
  ```
28
-
29
-
30
-
31
- array.append(imageView.image!)で強制アンラップしている箇所が気になりますね。imageView.imageをprintしてもらってnilは入ってない状況でしょうか?あとブレイクポイントを設定してどこまで処理が走っているかみてみるのもいいかもしれません。