检索火力存储图像上显示的tableView(SWIFT)

编程入门 行业动态 更新时间:2024-10-25 00:36:12
本文介绍了检索火力存储图像上显示的tableView(SWIFT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

扩展问题New火力地堡检索数据并换上的tableView(SWIFT)

通过移动火力地堡code到viewDidLoad中,邮政的所有文字部分可以curretly显示。但我还是不能把图像到的tableView。我有检查检索的图像,它是成功的,我想,我从火力地堡检索图像后数组中was't。

By moving the Firebase code to viewDidLoad, all the text part of Post can show curretly. But I still can't put the image to the tableView. I have check the images retrieving and it was success, I suppose that the images I retrieve from Firebase was't in the post array.

这是我的修复code:

this is my fix code:

import UIKit import Firebase import FirebaseStorage class timelineTableViewController: UITableViewController { var posts = [Post]() var databaseRef: FIRDatabaseReference! var storageRef: FIRStorageReference! override func viewDidLoad() { super.viewDidLoad() databaseRef = FIRDatabase.database().reference() storageRef = FIRStorage.storage().reference() let userPostRef = self.databaseRef.child("posts") userPostRef.queryOrderedByChild("time").observeEventType(.ChildAdded, withBlock: {(snapshot) in if let postAdd = snapshot.value as? NSDictionary{ let url = snapshot.value?["postPhoto"] as! String let userPhotoUrl = snapshot.value?["userPhoto"] as! String let myPost = Post(data: postAdd) FIRStorage.storage().referenceForURL(url).dataWithMaxSize(10 * 1024 * 1024, completion: { (data, error) in let postPhoto = UIImage(data: data!) }) FIRStorage.storage().referenceForURL(userPhotoUrl).dataWithMaxSize(10 * 1024 * 1024, completion: { (data, error) in let userPhoto = UIImage(data: data!) }) self.posts.insert(myPost, atIndex: 0) self.tableView.reloadData() } }) } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return posts.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellIdentifier = "postCell" let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)as! timelineTableViewCell //Dispatch the main thread here dispatch_async(dispatch_get_main_queue()) { cell.usernameLabel.text = self.posts[indexPath.row].username cell.postText.text = self.posts[indexPath.row].postText cell.timeLabel.text = self.posts[indexPath.row].time cell.postPhoto.image = self.posts[indexPath.row].postPhoto cell.userPhoto.image = self.posts[indexPath.row].userPhoto } return cell } }

和我的岗位结构希望它可以帮助!

And my struct of Post hope it might help!

struct Post { var username: String? var postText: String? var time: String? var userPhoto: UIImage? var postPhoto: UIImage? var url: String? var userPhotoUrl:String? init(data: NSDictionary) { username = data["username"] as? String postText = data["postText"] as? String time = data["time"]as? String userPhoto = data["userPhoto"] as? UIImage postPhoto = data["postPhoto"] as? UIImage url = data["postPhoto"] as? String userPhotoUrl = data["userPhoto"] as? String } }

希望能得到一些建议!

Hope can get some advice!

推荐答案

只需更改以下方法

func downloadPost(){ let userPostRef = self.databaseRef.child("posts") userPostRef.queryOrderedByChild("time").observeEventType(.ChildAdded, withBlock: {(snapshot) in if let postAdd = snapshot.value as? NSDictionary { print("\(snapshot.value)") let url = snapshot.value?["postPhoto"] as! String let userPhotoUrl = snapshot.value?["userPhoto"] as! String var myPost = Post(data: postAdd) //variable change to "var" because going to modify let url2 = NSURL(string: url) //postPhoto URL let data = NSData(contentsOfURL: url2!) // this URL convert into Data if data != nil { //Some time Data value will be nil so we need to validate such things myPost.postPhoto = UIImage(data: data!) } let url3 = NSURL(string: userPhotoUrl) //userPhoto URL let data2 = NSData(contentsOfURL: url3!) //Convert into data if data2 != nil { //check the data myPost.userPhoto = UIImage(data: data2!) //store in image } self.posts.insert(myPost, atIndex: 0) // then add the "myPost" Variable print(self.posts.count) } self.tableView.reloadData() // finally going to load the collection of data in tableview }) }

对其采取加载时间大大。因为图像转换并将其存储在mainqueue

its take loading time much. because converting the image and store it in the mainqueue.

另一个途径是存储URL和访问的tableview显示的URL。可以应用异步加载在ImageView的。

another ways is store the URL and access the URL on the tableview display. can apply the async loading in the imageview.

更多推荐

检索火力存储图像上显示的tableView(SWIFT)

本文发布于:2023-11-26 20:20:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:火力   图像   SWIFT   tableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!