【发布时间】:2018-01-13 05:56:58
【问题描述】:
所有图像都可以正常加载并显示其宽度与手机相同,其高度由照片的纵横比决定。最初除了第三张照片之外的所有照片都以这种方式加载,在滚动出图像视图并返回后,图像会正确调整大小,但最初图像只是在 scaleaspectFill 中,没有正确的宽度和高度。
import UIKit
import Firebase
import FirebaseStorageUI
class TableViewController: UITableViewController {
var images:[UIImage]! = [#imageLiteral(resourceName: "rininger_2.jpg")]
var imageURLS:[String] = [String]()
var listener:ListenerRegistration?
override func viewDidLoad() {
super.viewDidLoad()
/////////
listener = Firestore.firestore().collection("Posts").addSnapshotListener{
querySnapshot, error in
guard let snapshot = querySnapshot else {
print("Error fetching snapshots: \(error!)")
return
}
snapshot.documentChanges.forEach { diff in
if (diff.type == .added) {
print("New data: \(diff.document.data())")
}
if (diff.type == .modified) {
print("Modified city: \(diff.document.data())")
}
if (diff.type == .removed) {
print("Removed city: \(diff.document.data())")
}
// self.numberOfRows = snapshot.count
// Parse each post
//self.labels.append(newLabel)
//Update TableView
DispatchQueue.main.async {
//This would reload everything creating a flash
//self.tableView.reloadData()
guard let newImageURL = diff.document.data()["imageDownloadURL"] as? String else{
print("Failed to get image download URL")
return
}
//let newLabel = Post(snapshot:diff.document)
print("downloadURL: \(newImageURL)")
self.imageURLS.insert(newImageURL, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.insertRows(at: [indexPath], with: .top)
}
}
/////////
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return imageURLS.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Reuse", for: indexPath) as! TableViewCell
// Configure the cell...
///////////////////////
let downloadURL = URL(string: imageURLS[indexPath.row])
//cell.cellImageView.sd_setImage(with: downloadURL, completed:nil)
//cell.cellImageView.image = self.images[indexPath.row]
do{
let imageData = try Data(contentsOf: URL(string:(imageURLS[indexPath.row]))!)
cell.cellImageView.image = UIImage(data: imageData)
}
catch{
print("unable to load data: \(error)")
}
cell.cellImageView.contentMode = .scaleAspectFit
let aspectRatio = Float((cell.cellImageView?.image?.size.width)!/(cell.cellImageView?.image?.size.height)!)
tableView.rowHeight = CGFloat(Float(UIScreen.main.bounds.width)/aspectRatio)
///////////////////////
return cell
}
}
【问题讨论】:
标签: swift uitableview uiimageview uiimage