質問編集履歴

5

print文の修正

2017/04/06 10:07

投稿

udb
udb

スコア25

test CHANGED
File without changes
test CHANGED
@@ -580,11 +580,21 @@
580
580
 
581
581
  ```Swift
582
582
 
583
+ ここからdecodeData
584
+
585
+ /*60行ほどの画像をエンコードした文字列*/
586
+
587
+
588
+
589
+ ここからdecodedImagge
590
+
583
- Optional(<UIImage: 0x600000098790>, {1158, 1218})
591
+ Optional(<UIImage: 0x628000085370>, {1158, 1218})
584
-
592
+
585
- decodeData
593
+ Optional(nil)
586
-
594
+
587
- Could not cast value of type 'Swift.Optional<Any>' (0x7fa76fd14200) to 'Swift.String' (0x10677fae0).
595
+ Could not cast value of type 'Swift.Optional<Any>' (0x7fb8c0b17960) to 'Swift.String' (0x112448ae0).
596
+
597
+ (lldb)
588
598
 
589
599
  ```
590
600
 

4

こーどの全文追記

2017/04/06 10:06

投稿

udb
udb

スコア25

test CHANGED
File without changes
test CHANGED
@@ -2,34 +2,580 @@
2
2
 
3
3
 
4
4
 
5
- ``` Swift3.1
6
-
7
- //imageViewの接続
8
-
9
- @IBOutlet weak var dataImageView: UIImageView!
10
-
11
-
12
-
13
- //デコードしたデータをUIImage型へ変換してimageviewへ反映する
14
-
15
- let decodeData = (base64Encoded:dict["image"])
16
-
17
- print(decodeData)
18
-
19
- let decodedData = NSData(base64Encoded: decodeData as! String, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
20
-
21
- print("デコードされたイメージ")
22
-
23
- print(decodedData)
24
-
25
- let decodedImage = UIImage(data:decodedData! as Data)
26
-
27
- print(decodeImage)
28
-
29
- cell.dataImageView.image = decodedImage
5
+ ```Swift3
6
+
7
+ import UIKit
8
+
9
+ import Firebase
10
+
11
+
12
+
13
+ class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource {
14
+
15
+
16
+
17
+
18
+
19
+ var items = [NSDictionary]()
20
+
21
+
22
+
23
+ let refreshControl = UIRefreshControl()
24
+
25
+
26
+
27
+
28
+
29
+ var passImage:UIImage = UIImage()
30
+
31
+
32
+
33
+ var nowtableViewImage = UIImage()
34
+
35
+ var nowtableViewUserName = String()
36
+
37
+ var nowtableViewUserImage = UIImage()
38
+
39
+
40
+
41
+ @IBOutlet var tableView: UITableView!
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+ override func viewDidLoad() {
50
+
51
+ super.viewDidLoad()
52
+
53
+
54
+
55
+ if UserDefaults.standard.object(forKey: "check") != nil{
56
+
57
+
58
+
59
+ }else{
60
+
61
+
62
+
63
+ let loginViewController = self.storyboard?.instantiateViewController(withIdentifier: "login")
64
+
65
+ self.present(loginViewController!, animated: true, completion: nil)
66
+
67
+
68
+
69
+ }
70
+
71
+
72
+
73
+ tableView.delegate = self
74
+
75
+ tableView.dataSource = self
76
+
77
+
78
+
79
+ refreshControl.attributedTitle = NSAttributedString(string: "引っ張って更新")
80
+
81
+ refreshControl.addTarget(self, action:#selector(refresh), for:UIControlEvents.valueChanged)
82
+
83
+ tableView.addSubview(refreshControl)
84
+
85
+
86
+
87
+
88
+
89
+ items = [NSDictionary]()
90
+
91
+ loadAllData()
92
+
93
+ tableView.reloadData()
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+ }
102
+
103
+
104
+
105
+
106
+
107
+ func refresh(){
108
+
109
+
110
+
111
+ //データを読んでくる
112
+
113
+ //tableviewリロード
114
+
115
+
116
+
117
+ items = [NSDictionary]()
118
+
119
+ loadAllData()
120
+
121
+ tableView.reloadData()
122
+
123
+ refreshControl.endRefreshing()
124
+
125
+
126
+
127
+ }
128
+
129
+
130
+
131
+ //TableViewのデリゲートメソッド
132
+
133
+ func numberOfSections(in tableView: UITableView) -> Int {
134
+
135
+ return 1
136
+
137
+ }
138
+
139
+
140
+
141
+ //セルの数
142
+
143
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
144
+
145
+ return items.count
146
+
147
+ }
148
+
149
+
150
+
151
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
152
+
153
+
154
+
155
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
156
+
157
+
158
+
159
+ cell.selectionStyle = UITableViewCellSelectionStyle.none
160
+
161
+
162
+
163
+ let dict = items[(indexPath as NSIndexPath).row]
164
+
165
+
166
+
167
+
168
+
169
+ //プロフィール
170
+
171
+ let profileImageView = cell.viewWithTag(1) as! UIImageView
172
+
173
+
174
+
175
+ //デコードしたデータをUIImage型へ変換してimageviewへ反映する
176
+
177
+ let decodeData = (base64Encoded:dict["profileImage"])
178
+
179
+ let decodedData = NSData(base64Encoded: decodeData as! String, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
180
+
181
+ let decodedImage = UIImage(data:decodedData! as Data)
182
+
183
+ profileImageView.layer.cornerRadius = 8.0
184
+
185
+ profileImageView.clipsToBounds = true
186
+
187
+
188
+
189
+ profileImageView.image = decodedImage
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+ //ユーザー名
198
+
199
+ let userNameLabel = cell.viewWithTag(2) as! UILabel
200
+
201
+ userNameLabel.text = dict["username"] as? String
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+ //投稿画像
210
+
211
+ let postedImageView = cell.viewWithTag(3) as! UIImageView
212
+
213
+ //abv
214
+
215
+ let decodeData2 = (base64Encoded:dict["postImage"])
216
+
217
+ let decodedData2 = NSData(base64Encoded: decodeData2 as! String, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
218
+
219
+ let decodedImage2 = UIImage(data:decodedData2! as Data)
220
+
221
+ postedImageView.image = decodedImage2
222
+
223
+
224
+
225
+ //コメント
226
+
227
+ let commentTextView = cell.viewWithTag(4) as! UITextView
228
+
229
+ commentTextView.text = dict["comment"] as? String
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+ return cell
238
+
239
+ }
240
+
241
+
242
+
243
+ //データベースからデータをとってきて、配列の中に入れた
244
+
245
+ func loadAllData(){
246
+
247
+
248
+
249
+
250
+
251
+ // https://happystagram1.firebaseio.com/
252
+
253
+
254
+
255
+
256
+
257
+ UIApplication.shared.isNetworkActivityIndicatorVisible = true
258
+
259
+
260
+
261
+ let firebase = FIRDatabase.database().reference(fromURL: "https://instgram-ce25d.firebaseio.com/").child("Posts")
262
+
263
+
264
+
265
+
266
+
267
+ firebase.queryLimited(toLast: 10).observe(.value) { (snapshot,error) in
268
+
269
+ var tempItems = [NSDictionary]()
270
+
271
+ for item in(snapshot.children){
272
+
273
+
274
+
275
+ let child = item as! FIRDataSnapshot
276
+
277
+ let dict = child.value
278
+
279
+ tempItems.append(dict as! NSDictionary)
280
+
281
+
282
+
283
+
284
+
285
+ }
286
+
287
+
288
+
289
+ self.items = tempItems
290
+
291
+ self.items = self.items.reversed()
292
+
293
+ self.tableView.reloadData()
294
+
295
+
296
+
297
+
298
+
299
+ UIApplication.shared.isNetworkActivityIndicatorVisible = false
300
+
301
+
302
+
303
+ }
304
+
305
+ }
306
+
307
+
308
+
309
+
310
+
311
+ //
312
+
313
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
314
+
315
+
316
+
317
+
318
+
319
+ let dict = items[(indexPath as NSIndexPath).row]
320
+
321
+
322
+
323
+ //エンコードして取り出す
324
+
325
+ let decodeData = (base64Encoded:dict["profileImage"])
326
+
327
+
328
+
329
+ let decodedData = NSData(base64Encoded:decodeData as! String , options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
330
+
331
+ let decodedImage = UIImage(data:decodedData! as Data)
332
+
333
+ //abv
334
+
335
+ nowtableViewUserImage = decodedImage!
336
+
337
+
338
+
339
+ nowtableViewUserName = (dict["username"] as? String)!
340
+
341
+
342
+
343
+ //エンコードして取り出す
344
+
345
+ let decodeData2 = (base64Encoded:dict["postImage"])
346
+
347
+
348
+
349
+ let decodedData2 = NSData(base64Encoded:decodeData2 as! String , options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
350
+
351
+ let decodedImage2 = UIImage(data:decodedData2! as Data)
352
+
353
+ nowtableViewImage = decodedImage2!
354
+
355
+
356
+
357
+ performSegue(withIdentifier: "sns", sender: nil)
358
+
359
+
360
+
361
+
362
+
363
+
364
+
365
+ //postImage
366
+
367
+
368
+
369
+ }
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+ func openCamera(){
378
+
379
+
380
+
381
+
382
+
383
+ let sourceType:UIImagePickerControllerSourceType = UIImagePickerControllerSourceType.camera
384
+
385
+ // カメラが利用可能かチェック
386
+
387
+ if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
388
+
389
+ // インスタンスの作成
390
+
391
+ let cameraPicker = UIImagePickerController()
392
+
393
+ cameraPicker.sourceType = sourceType
394
+
395
+ cameraPicker.delegate = self
396
+
397
+ self.present(cameraPicker, animated: true, completion: nil)
398
+
399
+
400
+
401
+ }
402
+
403
+
404
+
405
+ }
406
+
407
+
408
+
409
+ func openPhoto(){
410
+
411
+
412
+
413
+
414
+
415
+ let sourceType:UIImagePickerControllerSourceType = UIImagePickerControllerSourceType.photoLibrary
416
+
417
+ // カメラが利用可能かチェック
418
+
419
+ if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
420
+
421
+ // インスタンスの作成
422
+
423
+ let cameraPicker = UIImagePickerController()
424
+
425
+ cameraPicker.sourceType = sourceType
426
+
427
+ cameraPicker.delegate = self
428
+
429
+ self.present(cameraPicker, animated: true, completion: nil)
430
+
431
+
432
+
433
+ }
434
+
435
+
436
+
437
+
438
+
439
+ }
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+ @IBAction func showCamera(_ sender: AnyObject) {
448
+
449
+
450
+
451
+
452
+
453
+ openCamera()
454
+
455
+
456
+
457
+ }
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+ @IBAction func showPhotos(_ sender: AnyObject) {
466
+
467
+
468
+
469
+ openPhoto()
470
+
471
+ }
472
+
473
+
474
+
475
+ func imagePickerController(_ imagePicker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
476
+
477
+
478
+
479
+ if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
480
+
481
+
482
+
483
+
484
+
485
+ passImage = pickedImage
486
+
487
+ performSegue(withIdentifier:"next",sender:nil)
488
+
489
+
490
+
491
+ }
492
+
493
+
494
+
495
+ //カメラ画面(アルバム画面)を閉じる処理
496
+
497
+ imagePicker.dismiss(animated: true, completion: nil)
498
+
499
+
500
+
501
+ }
502
+
503
+
504
+
505
+
506
+
507
+ override func prepare(for segue:UIStoryboardSegue,sender:Any?){
508
+
509
+
510
+
511
+
512
+
513
+ if(segue.identifier == "next"){
514
+
515
+
516
+
517
+
518
+
519
+ let editVC:EditViewController = segue.destination as! EditViewController
520
+
521
+ editVC.willEditImage = passImage
522
+
523
+ }
524
+
525
+
526
+
527
+ if(segue.identifier == "sns"){
528
+
529
+
530
+
531
+ let snsVC:SnsViewController = segue.destination as! SnsViewController
532
+
533
+ snsVC.detailImage = nowtableViewImage
534
+
535
+ snsVC.detailProfileImage = nowtableViewUserImage
536
+
537
+ snsVC.detailUserName = nowtableViewUserName
538
+
539
+
540
+
541
+ }
542
+
543
+
544
+
545
+ }
546
+
547
+
548
+
549
+
550
+
551
+
552
+
553
+
554
+
555
+ override func didReceiveMemoryWarning() {
556
+
557
+ super.didReceiveMemoryWarning()
558
+
559
+
560
+
561
+
562
+
563
+ }
564
+
565
+
566
+
567
+
568
+
569
+ }
570
+
571
+
30
572
 
31
573
  ```
32
574
 
575
+
576
+
577
+
578
+
33
579
  出力されるエラー
34
580
 
35
581
  ```Swift

3

追記

2017/04/05 22:44

投稿

udb
udb

スコア25

test CHANGED
File without changes
test CHANGED
@@ -24,6 +24,8 @@
24
24
 
25
25
  let decodedImage = UIImage(data:decodedData! as Data)
26
26
 
27
+ print(decodeImage)
28
+
27
29
  cell.dataImageView.image = decodedImage
28
30
 
29
31
  ```

2

エラーに関して記述

2017/04/05 21:58

投稿

udb
udb

スコア25

test CHANGED
File without changes
test CHANGED
@@ -4,11 +4,23 @@
4
4
 
5
5
  ``` Swift3.1
6
6
 
7
+ //imageViewの接続
8
+
9
+ @IBOutlet weak var dataImageView: UIImageView!
10
+
11
+
12
+
7
13
  //デコードしたデータをUIImage型へ変換してimageviewへ反映する
8
14
 
9
15
  let decodeData = (base64Encoded:dict["image"])
10
16
 
17
+ print(decodeData)
18
+
11
19
  let decodedData = NSData(base64Encoded: decodeData as! String, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
20
+
21
+ print("デコードされたイメージ")
22
+
23
+ print(decodedData)
12
24
 
13
25
  let decodedImage = UIImage(data:decodedData! as Data)
14
26
 
@@ -16,6 +28,20 @@
16
28
 
17
29
  ```
18
30
 
31
+ 出力されるエラー
32
+
33
+ ```Swift
34
+
35
+ Optional(<UIImage: 0x600000098790>, {1158, 1218})
36
+
37
+ decodeData
38
+
39
+ Could not cast value of type 'Swift.Optional<Any>' (0x7fa76fd14200) to 'Swift.String' (0x10677fae0).
40
+
41
+ ```
42
+
43
+
44
+
19
45
 
20
46
 
21
47
  Xcode8.3/Swift3で行なっていますが、解決方法にめどがつきません。どなたかご教示をお願いします。

1

誤字

2017/04/05 21:55

投稿

udb
udb

スコア25

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- Firebaseに保存されたエンコードされた画像データをSwiftでデコードして表示させようとしたらエラーの表示が表示がないのに関わらずエラーが起きてアプリがクラッシュしてしまいます。
1
+ Firebaseに保存されたエンコードされた画像データをSwiftでデコードして表示させようとしたらエラーの表示がないのに関わらずエラーが起きてアプリがクラッシュしてしまいます。
2
2
 
3
3
 
4
4