我很难将Firebase存储中的图像检索到我的TableView
。感谢任何帮助。以下是我的代码:
tableviewcell代码:
class HomeFeedCell : UITableViewCell {
Var feeds = [HomeFeed]()
var dbRef :FIRDatabaseReference!
var storage : FIRStorage!
@IBOutlet weak var Name: UILabel!
@IBOutlet weak var Location: UILabel!
@IBOutlet weak var Type: UILabel!
@IBOutlet weak var FeedImage: UIImageView!
func configureCellWith(product : HomeFeed)
{
Name.text? = product.name
Location.text? = product.location
Type.text? = product.type
func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
// Configure the view for the selected state
}
}
我的桌面视图:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! HomeFeedCell
let details = feeds[indexPath.row]
cell.configureCellWith(details)
return cell
}
我能够成功显示名称,位置和类型细节,但我只是坚持检索图像。下面是我的结构文件:
struct HomeFeed {
let key:String!
let name:String!
let location:String!
let review:String!
let type:String!
let photoURL:String
答案 0 :(得分:1)
你可能想做这样的事情:
在表格视图单元格中下载(基于传入单元格的名称或其他一些唯一标识符):
// Create a storage reference from the URL
let storageRef = storage.reference("name/of/my/object/to/download.jpg")
// Download the data, assuming a max size of 1MB (you can change this as necessary)
storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
// Create a UIImage, add it to the array
let pic = UIImage(data: data)
FeedImage.image = pic
})
有关详细信息,请参阅Zero to App: Develop with Firebase,以及associated source code,了解如何执行此操作的实际示例。
答案 1 :(得分:0)
有两种方法可以从网址,异步和同步中获取您的图片,我已在下面介绍了使用
<强>同步方式:强>
if let filePath = Bundle.main.pathForResource("imageName", ofType: "jpg"), image = UIImage(contentsOfFile: filePath)
{
imageView.contentMode = .scaleAspectFit
imageView.image = image
}
<强>异步:强>
使用完成处理程序创建一个方法以从您的网址获取图像数据
func getDataFromUrl(url: URL, completion: ((data: Data?, response: URLResponse?, error: NSError? ) -> Void)) {
URLSession.shared.dataTask(with: url) {
(data, response, error) in
completion(data: data, response: response, error: error)
}.resume()
}
创建下载图像的方法(启动任务)
func downloadImage(url: URL){
print("Download Started")
getDataFromUrl(url: url) { (data, response, error) in
guard let data = data where error == nil else { return }
DispatchQueue.main.async() { () -> Void in
print(response?.suggestedFilename ?? url.lastPathComponent ?? "")
print("Download Finished")
self.imageView.image = UIImage(data: data)
}
}
}
<强>用法:强>
override func viewDidLoad() {
super.viewDidLoad()
print("Begin of code")
if let checkedUrl = URL(string: "http://www.apple.com/euro/ios/ios8/a/generic/images/og.png") {
imageView.contentMode = .scaleAspectFit
downloadImage(url: checkedUrl)
}
print("End of code. The image will continue downloading in the background and it will be loaded when finished.")
}
希望这会对你有帮助!