为什么 URL 在尝试转换图像时无法正常工作?
Why URL not working correctly when tried to turn image?
我对 Firebase 有疑问。我的数据库中有图像 URL,我在 collectionView 单元格中显示它们,但在这一点上我遇到了一些问题,即某些图像未正确加载,它们的 URL 不同,但图像相同。我尝试了很多东西,但我无法解决。我的 URL 以 'https' 和 App Transport Security Setting 开头,Allow Arbitrary Loads = YES。所以这些都没有解决我的问题。这是我的代码,它是 firebase 并将此 URL 添加到 imageViews。请帮我!谢谢!
func firebaseCon() {
let ref = Database.database().reference().child("cells")
ref.observe(.childAdded) { (snapshot) in
if let dict = snapshot.value as? [String: AnyObject] {
let dataCon = ItemCellImage()
dataCon.itemImageName = dict["itemimagename"] as? String
dataCon.itemTitleLabel = dict["itemimagelabel"] as? String
//print(dataCon.itemImageName, dataCon.itemTitleLabel)
self.itemler.append(dataCon)
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
}
这里从数据到图像:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! itemsCell
let dataGelen = itemler[indexPath.row]
cell.titleLabel.text = dataGelen.itemTitleLabel
if let cellDataImage = dataGelen.itemImageName {
let url = URL(string: cellDataImage)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
cell.itemsimageView.image = UIImage(data: data!)
self.imageDeneme = UIImage(data: data!)!
}
}.resume()
}
return cell
}
当我尝试从 firebase 更改图像 url 时,其中一些可以正常工作,而另一些只显示以前的图像。有没有办法在每次更改时都显示完全正确的图像?
对于由于 Reusing
概念引起的此类问题,您可以使用 cells
提供的 prepareForReuse()
函数。
override func prepareForReuse() {
yourImageView.image = nil //Or any placeholder image
}
此函数在 OS is about to reuse your cell
时调用,因此您可以将其用作一种重置您的单元格外观的方法。
我对 Firebase 有疑问。我的数据库中有图像 URL,我在 collectionView 单元格中显示它们,但在这一点上我遇到了一些问题,即某些图像未正确加载,它们的 URL 不同,但图像相同。我尝试了很多东西,但我无法解决。我的 URL 以 'https' 和 App Transport Security Setting 开头,Allow Arbitrary Loads = YES。所以这些都没有解决我的问题。这是我的代码,它是 firebase 并将此 URL 添加到 imageViews。请帮我!谢谢!
func firebaseCon() {
let ref = Database.database().reference().child("cells")
ref.observe(.childAdded) { (snapshot) in
if let dict = snapshot.value as? [String: AnyObject] {
let dataCon = ItemCellImage()
dataCon.itemImageName = dict["itemimagename"] as? String
dataCon.itemTitleLabel = dict["itemimagelabel"] as? String
//print(dataCon.itemImageName, dataCon.itemTitleLabel)
self.itemler.append(dataCon)
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
}
这里从数据到图像:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! itemsCell
let dataGelen = itemler[indexPath.row]
cell.titleLabel.text = dataGelen.itemTitleLabel
if let cellDataImage = dataGelen.itemImageName {
let url = URL(string: cellDataImage)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
cell.itemsimageView.image = UIImage(data: data!)
self.imageDeneme = UIImage(data: data!)!
}
}.resume()
}
return cell
}
当我尝试从 firebase 更改图像 url 时,其中一些可以正常工作,而另一些只显示以前的图像。有没有办法在每次更改时都显示完全正确的图像?
对于由于 Reusing
概念引起的此类问题,您可以使用 cells
提供的 prepareForReuse()
函数。
override func prepareForReuse() {
yourImageView.image = nil //Or any placeholder image
}
此函数在 OS is about to reuse your cell
时调用,因此您可以将其用作一种重置您的单元格外观的方法。