質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

Cloud Firestore

Cloud Firestore は、自動スケーリングと高性能を実現し、アプリケーション開発を簡素化するように構築された NoSQLドキュメントデータベースです。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

930閲覧

[swift]firebaseを利用した画像の読み取りについて

pit11511

総合スコア0

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

Cloud Firestore

Cloud Firestore は、自動スケーリングと高性能を実現し、アプリケーション開発を簡素化するように構築された NoSQLドキュメントデータベースです。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2021/12/06 05:13

[swift]firebaseを利用した画像の読み取りについて

SNSにおいてfirebaseを利用して、画像を読み取り表示しようとしています。仕組みとしては、フォローしている人のIDと同じ画像を検索し、tableviewに表示しています。tablecellに関してはXIBファイルを利用しています。その際に、アプリを起動してすぐには、画像がうまく表示されず、別機能のユーザーの画像変更処理を行うと、画像が変更できます。これを起動してすぐに表示されるようにしたいと考えています。

発生している問題・エラーメッセージ

エラーメッセージはありません。

エラーメッセージ

該当のソースコード

houseviewcontroller

1import UIKit 2import Firebase 3import FirebaseOAuthUI 4import FirebaseStorageUI 5 6class houseViewController: UIViewController { 7 var appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate 8 @IBOutlet weak var tableView: UITableView! 9 let storage = Storage.storage() 10 let df = DateFormatter() 11 var db: Firestore! 12 let settings = FirestoreSettings() 13 var followpeople = [Any]() { 14 didSet{ 15 self.followpost = [] 16 for i in self.followpeople{ 17 18 self.db.collection("post").whereField("hostid", isEqualTo: i) 19 .getDocuments() { (querySnapshot, err) in 20 if let err = err { 21 print("Error getting documents: (err)") 22 } else { 23 for document in querySnapshot!.documents { 24 self.followpost.append(document.data()) 25 print(self.followpost) 26 } 27 } 28 } 29 self.tableView.reloadData() 30 } 31 } 32 } 33 34 35 var followpost = [[String:Any]](){didSet{ 36 self.tableView.reloadData() 37 }} 38 override func loadView() { 39 super.loadView() 40 Firestore.firestore().settings = settings 41 db = Firestore.firestore() 42 let user = Auth.auth().currentUser 43 if let user = user { 44 // The user's ID, unique to the Firebase project. 45 // Do NOT use this value to authenticate with your backend server, 46 // if you have one. Use getTokenWithCompletion:completion: instead. 47 let uid = user.uid 48 let email = user.email 49 let photoURL = user.photoURL 50 let displayName = user.displayName 51 var multiFactorString = "MultiFactor: " 52 for info in user.multiFactor.enrolledFactors { 53 multiFactorString += info.displayName ?? "[DispayName]" 54 multiFactorString += " " 55 } 56 } 57 58 let ref = db.collection("users").document(user!.uid).collection("follow").document("follow") 59 ref.getDocument{ (document, error) in 60 if let document = document { 61 for i in document.data()!{ 62 self.followpeople.append(i.value) 63 print(self.followpeople) 64 } 65 }else{ 66 print("Document does not exist") 67 } 68 } 69 } 70 override func viewDidLoad() { 71 super.viewDidLoad() 72 let nib = UINib(nibName: "TableViewCell", bundle: nil) 73 tableView.register(nib, forCellReuseIdentifier: "postcell") 74 // Do any additional setup after loading the view. 75 } 76 77 /* 78 // MARK: - Navigation 79 80 // In a storyboard-based application, you will often want to do a little preparation before navigation 81 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 82 // Get the new view controller using segue.destination. 83 // Pass the selected object to the new view controller. 84 } 85 */ 86 87} 88extension houseViewController:UITableViewDelegate,UITableViewDataSource{ 89 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 90 return followpost.count 91 } 92 93 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 94 let cell = tableView.dequeueReusableCell(withIdentifier: "postcell", for: indexPath) as! TableViewCell 95 cell.post = followpost[indexPath.row] 96 cell.updateUI() 97 return cell 98 cell.updateUI() 99 } 100 101 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 102 appDelegate.selectpost = followpost[indexPath.row] 103 performSegue(withIdentifier: "momorere", sender: nil) 104 } 105} 106

UItableViewCell

1import UIKit 2import Firebase 3import FirebaseStorageUI 4import FirebaseOAuthUI 5class TableViewCell: UITableViewCell { 6 7 @IBOutlet weak var userProfilePic: UIImageView! 8 @IBOutlet weak var userName: UILabel! 9 @IBOutlet weak var postDay: UILabel! 10 @IBOutlet weak var postPic: UIImageView! 11 @IBOutlet weak var overview: UILabel! 12 @IBOutlet weak var join: UIButton! 13 @IBOutlet weak var comment: UIButton! 14 private var joindid: Bool = false 15 //FIREBASE関連 16 var db: Firestore! 17 let settings = FirestoreSettings() 18 let storage = Storage.storage() 19 20 21 var post = [String:Any](){ 22 didSet{ 23 updateUI() 24 } 25 } 26 func updateUI(){ 27 //画像を丸める 28 userProfilePic.layer.cornerRadius = userProfilePic.layer.bounds.width/2 29 postPic.layer.cornerRadius = 5.0 30 postPic.clipsToBounds = true 31 32 33 let storageRef = StorageReference() 34 let postpicRef = storageRef.child(post["photo1"]! as! String) 35 postpicRef.downloadURL { url, error in 36 if let error = error { 37 // Handle any errors 38 } else { 39 let imageView: UIImageView = self.postPic 40 41 // Placeholder image 42 let placeholderImage = UIImage(named: "placeholder.jpg") 43 44 // Load the image using SDWebImage 45 imageView.sd_setImage(with: postpicRef, placeholderImage: placeholderImage) 46 } 47 } 48 49 50 let userpicRef = storageRef.child(post["userImageURL"]! as! String) 51 userpicRef.downloadURL { url, error in 52 if let error = error { 53 // Handle any errors 54 } else { 55 let imageView: UIImageView = self.userProfilePic 56 // Placeholder image 57 let placeholderImage = UIImage(named: "placeholder2.jpg") 58 59 // Load the image using SDWebImage 60 imageView.sd_setImage(with: userpicRef, placeholderImage: placeholderImage) 61 } 62 } 63 userName.text! = post["hostname"] as! String 64 postDay.text! = post["postday"] as! String 65 66 overview.text! = post["title"] as! String 67 configureBottunAppearence() 68 } 69 private func configureBottunAppearence(){ 70 //参加ボタン 71 join.layer.cornerRadius = 3.0 72 join.layer.borderWidth = 2.0 73 join.layer.borderColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1) 74 join.backgroundColor = #colorLiteral(red: 1, green: 0.5964409709, blue: 0.3063230217, alpha: 1) 75 join.tintColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) 76 77 //参加ボタン 78 comment.layer.cornerRadius = 3.0 79 comment.layer.borderWidth = 2.0 80 comment.layer.borderColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1) 81 comment.backgroundColor = #colorLiteral(red: 1, green: 0.5964409709, blue: 0.3063230217, alpha: 1) 82 comment.tintColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) 83 84 } 85 override func awakeFromNib() { 86 super.awakeFromNib() 87 // Initialization code 88 } 89 90 override func setSelected(_ selected: Bool, animated: Bool) { 91 super.setSelected(selected, animated: animated) 92 93 // Configure the view for the selected state 94 } 95 96 @IBAction func commentButtonClicked(_ sender: Any) { 97 98 } 99 @IBAction func joinButtonClicked(_ sender: Any) { 100 101 102 } 103 104}

試したこと

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問