【问题标题】:Loading images from Firebase in TableViewCell Swift在 TableViewCell Swift 中从 Firebase 加载图像
【发布时间】:2017-12-22 21:46:50
【问题描述】:

我正在尝试在我的 TableView 单元中从我的 Firebase 数据库加载图像。 我正在使用 AlamofireImage。

@IBOutlet weak var tableView: UITableView!

var storiesArray = [Stories]()

override func viewDidLoad() {
    super.viewDidLoad()

    retrieveStories()
}

我在 viewDidLoad() 中调用的这个函数中获取 imageDownloadURL

 func retrieveStories() {

    let storiesDB = Database.database().reference().child("storyDetails")

    storiesDB.observe(.childAdded) { (snapshot) in

        let snapshotValue = snapshot.value as! Dictionary<String,String>
        let autor = snapshotValue["autor"]!
        let genre = snapshotValue["genre"]!
        let titel = snapshotValue["titel"]!
        let downloadURL = snapshotValue["imageDownloadURL"]


        let story = Stories()
        story.genre = genre
        story.titel = titel
        story.autor = autor
        story.downloadURL = downloadURL

        self.storiesArray.append(story)

        DispatchQueue.main.async(execute: {
            self.tableView.reloadData()
        })

    }
} 

获得所有数据后,我想从我现在在 storiesArray.downloadURL 中拥有的 url 下载图像

我在 cellForRowAtIndexPath 中这样做

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! StoryDetailCell

    if storiesArray[indexPath.row].downloadURL != nil {
            Alamofire.request(indexPathRow.downloadURL!).responseImage { response in
                debugPrint(response)

                print(response.request as Any)
                print(response.response as Any)
                debugPrint(response.result)

                if let image = response.result.value {
                    print("image downloaded: \(image)")
                    DispatchQueue.main.async {
                        cell.storyImage.image = image
                    }

                }
            }
        } else if indexPathRow.downloadURL == nil {
            cell.storyImage.image = #imageLiteral(resourceName: "standard")
    }

只有当前显示单元格时才会加载图像。这会导致很多问题。一个问题是,如果我滚动的速度比加载图像的速度快,它将显示在另一个单元格中。

我想知道如何处理这种情况。

【问题讨论】:

  • 这并不是真正的答案,但 SDWebImage 会处理一切。此外,您最好也有一个占位符图像。

标签: ios swift uitableview firebase alamofireimage


【解决方案1】:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! StoryDetailCell

    cell.storyImage.image = nil

    cell.tag += 1
    let tag = cell.tag

    if storiesArray[indexPath.row].downloadURL != nil {
        Alamofire.request(indexPathRow.downloadURL!).responseImage { response in

            if let image = response.result.value {
                if cell.tag == tag {
                    DispatchQueue.main.async {
                        cell.storyImage.image = image
                    }
                }
            }
        }
    } else if indexPathRow.downloadURL == nil {
        cell.storyImage.image = #imageLiteral(resourceName: "standard")
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多