質問編集履歴
5
print文の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -289,9 +289,14 @@
|
|
289
289
|
|
290
290
|
出力されるエラー
|
291
291
|
```Swift
|
292
|
+
ここからdecodeData
|
293
|
+
/*60行ほどの画像をエンコードした文字列*/
|
294
|
+
|
295
|
+
ここからdecodedImagge
|
292
|
-
Optional(<UIImage:
|
296
|
+
Optional(<UIImage: 0x628000085370>, {1158, 1218})
|
293
|
-
|
297
|
+
Optional(nil)
|
294
|
-
Could not cast value of type 'Swift.Optional<Any>' (
|
298
|
+
Could not cast value of type 'Swift.Optional<Any>' (0x7fb8c0b17960) to 'Swift.String' (0x112448ae0).
|
299
|
+
(lldb)
|
295
300
|
```
|
296
301
|
|
297
302
|
|
4
こーどの全文追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,19 +1,292 @@
|
|
1
1
|
Firebaseに保存されたエンコードされた画像データをSwiftでデコードして表示させようとしたらエラーの表示がないのに関わらずエラーが起きてアプリがクラッシュしてしまいます。
|
2
2
|
|
3
|
-
```
|
3
|
+
```Swift3
|
4
|
+
import UIKit
|
4
|
-
|
5
|
+
import Firebase
|
5
|
-
@IBOutlet weak var dataImageView: UIImageView!
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
7
|
+
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource {
|
8
|
+
|
9
|
+
|
10
|
+
var items = [NSDictionary]()
|
11
|
+
|
12
|
+
let refreshControl = UIRefreshControl()
|
13
|
+
|
14
|
+
|
15
|
+
var passImage:UIImage = UIImage()
|
16
|
+
|
17
|
+
var nowtableViewImage = UIImage()
|
18
|
+
var nowtableViewUserName = String()
|
19
|
+
var nowtableViewUserImage = UIImage()
|
20
|
+
|
21
|
+
@IBOutlet var tableView: UITableView!
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
override func viewDidLoad() {
|
26
|
+
super.viewDidLoad()
|
27
|
+
|
28
|
+
if UserDefaults.standard.object(forKey: "check") != nil{
|
29
|
+
|
30
|
+
}else{
|
31
|
+
|
32
|
+
let loginViewController = self.storyboard?.instantiateViewController(withIdentifier: "login")
|
33
|
+
self.present(loginViewController!, animated: true, completion: nil)
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
tableView.delegate = self
|
38
|
+
tableView.dataSource = self
|
39
|
+
|
40
|
+
refreshControl.attributedTitle = NSAttributedString(string: "引っ張って更新")
|
41
|
+
refreshControl.addTarget(self, action:#selector(refresh), for:UIControlEvents.valueChanged)
|
42
|
+
tableView.addSubview(refreshControl)
|
43
|
+
|
44
|
+
|
45
|
+
items = [NSDictionary]()
|
46
|
+
loadAllData()
|
47
|
+
tableView.reloadData()
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
func refresh(){
|
55
|
+
|
56
|
+
//データを読んでくる
|
57
|
+
//tableviewリロード
|
58
|
+
|
59
|
+
items = [NSDictionary]()
|
60
|
+
loadAllData()
|
61
|
+
tableView.reloadData()
|
62
|
+
refreshControl.endRefreshing()
|
63
|
+
|
64
|
+
}
|
65
|
+
|
66
|
+
//TableViewのデリゲートメソッド
|
67
|
+
func numberOfSections(in tableView: UITableView) -> Int {
|
68
|
+
return 1
|
69
|
+
}
|
70
|
+
|
71
|
+
//セルの数
|
72
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
73
|
+
return items.count
|
74
|
+
}
|
75
|
+
|
76
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
77
|
+
|
78
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
|
79
|
+
|
80
|
+
cell.selectionStyle = UITableViewCellSelectionStyle.none
|
81
|
+
|
82
|
+
let dict = items[(indexPath as NSIndexPath).row]
|
83
|
+
|
84
|
+
|
85
|
+
//プロフィール
|
86
|
+
let profileImageView = cell.viewWithTag(1) as! UIImageView
|
87
|
+
|
88
|
+
//デコードしたデータをUIImage型へ変換してimageviewへ反映する
|
89
|
+
let decodeData = (base64Encoded:dict["profileImage"])
|
90
|
+
let decodedData = NSData(base64Encoded: decodeData as! String, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
|
91
|
+
let decodedImage = UIImage(data:decodedData! as Data)
|
92
|
+
profileImageView.layer.cornerRadius = 8.0
|
93
|
+
profileImageView.clipsToBounds = true
|
94
|
+
|
95
|
+
profileImageView.image = decodedImage
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
//ユーザー名
|
100
|
+
let userNameLabel = cell.viewWithTag(2) as! UILabel
|
101
|
+
userNameLabel.text = dict["username"] as? String
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
//投稿画像
|
106
|
+
let postedImageView = cell.viewWithTag(3) as! UIImageView
|
107
|
+
//abv
|
108
|
+
let decodeData2 = (base64Encoded:dict["postImage"])
|
109
|
+
let decodedData2 = NSData(base64Encoded: decodeData2 as! String, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
|
110
|
+
let decodedImage2 = UIImage(data:decodedData2! as Data)
|
111
|
+
postedImageView.image = decodedImage2
|
112
|
+
|
113
|
+
//コメント
|
114
|
+
let commentTextView = cell.viewWithTag(4) as! UITextView
|
115
|
+
commentTextView.text = dict["comment"] as? String
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
return cell
|
120
|
+
}
|
121
|
+
|
122
|
+
//データベースからデータをとってきて、配列の中に入れた
|
123
|
+
func loadAllData(){
|
124
|
+
|
125
|
+
|
126
|
+
// https://happystagram1.firebaseio.com/
|
127
|
+
|
128
|
+
|
129
|
+
UIApplication.shared.isNetworkActivityIndicatorVisible = true
|
130
|
+
|
131
|
+
let firebase = FIRDatabase.database().reference(fromURL: "https://instgram-ce25d.firebaseio.com/").child("Posts")
|
132
|
+
|
133
|
+
|
134
|
+
firebase.queryLimited(toLast: 10).observe(.value) { (snapshot,error) in
|
135
|
+
var tempItems = [NSDictionary]()
|
136
|
+
for item in(snapshot.children){
|
137
|
+
|
138
|
+
let child = item as! FIRDataSnapshot
|
139
|
+
let dict = child.value
|
140
|
+
tempItems.append(dict as! NSDictionary)
|
141
|
+
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
self.items = tempItems
|
146
|
+
self.items = self.items.reversed()
|
147
|
+
self.tableView.reloadData()
|
148
|
+
|
149
|
+
|
150
|
+
UIApplication.shared.isNetworkActivityIndicatorVisible = false
|
151
|
+
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
//
|
157
|
+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
158
|
+
|
159
|
+
|
160
|
+
let dict = items[(indexPath as NSIndexPath).row]
|
161
|
+
|
162
|
+
//エンコードして取り出す
|
163
|
+
let decodeData = (base64Encoded:dict["profileImage"])
|
164
|
+
|
165
|
+
let decodedData = NSData(base64Encoded:decodeData as! String , options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
|
166
|
+
let decodedImage = UIImage(data:decodedData! as Data)
|
167
|
+
//abv
|
168
|
+
nowtableViewUserImage = decodedImage!
|
169
|
+
|
170
|
+
nowtableViewUserName = (dict["username"] as? String)!
|
171
|
+
|
172
|
+
//エンコードして取り出す
|
173
|
+
let decodeData2 = (base64Encoded:dict["postImage"])
|
174
|
+
|
175
|
+
let decodedData2 = NSData(base64Encoded:decodeData2 as! String , options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
|
176
|
+
let decodedImage2 = UIImage(data:decodedData2! as Data)
|
177
|
+
nowtableViewImage = decodedImage2!
|
178
|
+
|
179
|
+
performSegue(withIdentifier: "sns", sender: nil)
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
//postImage
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
func openCamera(){
|
190
|
+
|
191
|
+
|
192
|
+
let sourceType:UIImagePickerControllerSourceType = UIImagePickerControllerSourceType.camera
|
193
|
+
// カメラが利用可能かチェック
|
194
|
+
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
|
195
|
+
// インスタンスの作成
|
196
|
+
let cameraPicker = UIImagePickerController()
|
197
|
+
cameraPicker.sourceType = sourceType
|
198
|
+
cameraPicker.delegate = self
|
199
|
+
self.present(cameraPicker, animated: true, completion: nil)
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
func openPhoto(){
|
206
|
+
|
207
|
+
|
208
|
+
let sourceType:UIImagePickerControllerSourceType = UIImagePickerControllerSourceType.photoLibrary
|
209
|
+
// カメラが利用可能かチェック
|
210
|
+
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
|
211
|
+
// インスタンスの作成
|
212
|
+
let cameraPicker = UIImagePickerController()
|
213
|
+
cameraPicker.sourceType = sourceType
|
214
|
+
cameraPicker.delegate = self
|
215
|
+
self.present(cameraPicker, animated: true, completion: nil)
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
}
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
@IBAction func showCamera(_ sender: AnyObject) {
|
225
|
+
|
226
|
+
|
227
|
+
openCamera()
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
@IBAction func showPhotos(_ sender: AnyObject) {
|
234
|
+
|
235
|
+
openPhoto()
|
236
|
+
}
|
237
|
+
|
238
|
+
func imagePickerController(_ imagePicker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
|
239
|
+
|
240
|
+
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
|
241
|
+
|
242
|
+
|
243
|
+
passImage = pickedImage
|
244
|
+
performSegue(withIdentifier:"next",sender:nil)
|
245
|
+
|
246
|
+
}
|
247
|
+
|
248
|
+
//カメラ画面(アルバム画面)を閉じる処理
|
249
|
+
imagePicker.dismiss(animated: true, completion: nil)
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
override func prepare(for segue:UIStoryboardSegue,sender:Any?){
|
255
|
+
|
256
|
+
|
257
|
+
if(segue.identifier == "next"){
|
258
|
+
|
259
|
+
|
260
|
+
let editVC:EditViewController = segue.destination as! EditViewController
|
261
|
+
editVC.willEditImage = passImage
|
262
|
+
}
|
263
|
+
|
264
|
+
if(segue.identifier == "sns"){
|
265
|
+
|
266
|
+
let snsVC:SnsViewController = segue.destination as! SnsViewController
|
267
|
+
snsVC.detailImage = nowtableViewImage
|
268
|
+
snsVC.detailProfileImage = nowtableViewUserImage
|
269
|
+
snsVC.detailUserName = nowtableViewUserName
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
278
|
+
override func didReceiveMemoryWarning() {
|
279
|
+
super.didReceiveMemoryWarning()
|
280
|
+
|
281
|
+
|
282
|
+
}
|
283
|
+
|
284
|
+
|
285
|
+
}
|
286
|
+
|
16
287
|
```
|
288
|
+
|
289
|
+
|
17
290
|
出力されるエラー
|
18
291
|
```Swift
|
19
292
|
Optional(<UIImage: 0x600000098790>, {1158, 1218})
|
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,6 +11,7 @@
|
|
11
11
|
print("デコードされたイメージ")
|
12
12
|
print(decodedData)
|
13
13
|
let decodedImage = UIImage(data:decodedData! as Data)
|
14
|
+
print(decodeImage)
|
14
15
|
cell.dataImageView.image = decodedImage
|
15
16
|
```
|
16
17
|
出力されるエラー
|
2
エラーに関して記述
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,11 +1,24 @@
|
|
1
1
|
Firebaseに保存されたエンコードされた画像データをSwiftでデコードして表示させようとしたらエラーの表示がないのに関わらずエラーが起きてアプリがクラッシュしてしまいます。
|
2
2
|
|
3
3
|
``` Swift3.1
|
4
|
+
//imageViewの接続
|
5
|
+
@IBOutlet weak var dataImageView: UIImageView!
|
6
|
+
|
4
7
|
//デコードしたデータをUIImage型へ変換してimageviewへ反映する
|
5
8
|
let decodeData = (base64Encoded:dict["image"])
|
9
|
+
print(decodeData)
|
6
10
|
let decodedData = NSData(base64Encoded: decodeData as! String, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
|
11
|
+
print("デコードされたイメージ")
|
12
|
+
print(decodedData)
|
7
13
|
let decodedImage = UIImage(data:decodedData! as Data)
|
8
14
|
cell.dataImageView.image = decodedImage
|
9
15
|
```
|
16
|
+
出力されるエラー
|
17
|
+
```Swift
|
18
|
+
Optional(<UIImage: 0x600000098790>, {1158, 1218})
|
19
|
+
decodeData
|
20
|
+
Could not cast value of type 'Swift.Optional<Any>' (0x7fa76fd14200) to 'Swift.String' (0x10677fae0).
|
21
|
+
```
|
10
22
|
|
23
|
+
|
11
24
|
Xcode8.3/Swift3で行なっていますが、解決方法にめどがつきません。どなたかご教示をお願いします。
|
1
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Firebaseに保存されたエンコードされた画像データをSwiftでデコードして表示させようとしたらエラーの表示が
|
1
|
+
Firebaseに保存されたエンコードされた画像データをSwiftでデコードして表示させようとしたらエラーの表示がないのに関わらずエラーが起きてアプリがクラッシュしてしまいます。
|
2
2
|
|
3
3
|
``` Swift3.1
|
4
4
|
//デコードしたデータをUIImage型へ変換してimageviewへ反映する
|