为啥当我滚动浏览 UITableView 时图像会不断调整大小?
Posted
技术标签:
【中文标题】为啥当我滚动浏览 UITableView 时图像会不断调整大小?【英文标题】:Why do the images keep on resizing as I scroll through my UITableView?为什么当我滚动浏览 UITableView 时图像会不断调整大小? 【发布时间】: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
【问题讨论】:
【参考方案1】:确保UIImageView
上的clipsToBounds
属性设置为true
。
【讨论】:
感谢您的回复,不幸的是它不是那么简单,我已尝试将约束设置为对所有范围都归零。我相信它与队列有关。以上是关于为啥当我滚动浏览 UITableView 时图像会不断调整大小?的主要内容,如果未能解决你的问题,请参考以下文章
我的 UITableView 不会向下滚动到数据的末尾!为啥?
为啥 UITableView 只在输入第一个字符时自动滚动?