[英]error while loading images asynchronously from firebase
我一直試圖安靜地異步加載圖像。 當我為此創建一個新類並構建並運行該應用程序時,運行時日志中會出現錯誤
// log
Failed to set (borderWidth) user defined inspected property on ...
could not set nil as the value for the key borderWidth.
could not set nil as the value for the key cornerRadius.
用擴展名而不是類很好,圖像異步加載,但是這樣,我將無法確保在圖像視圖中加載了正確的圖像。
// cell for item at
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "subscribeCell", for: indexPath) as! subscribeViewCell
cell.userID = self.users[indexPath.row].userId
cell.profileName.text = self.users[indexPath.row].fullName
cell.profileImage.setupWithImageUrl(imageUrl: self.users[indexPath.row].ImagePath)
checkFollowers(indexPath : indexPath)
return cell
}
我在同一個視圖控制器中使用的類:
類下載圖片:
private var imageCache = [String:UIImage]()
class CustomImageView : UIImageView {
var lastImageUrl : String?
func setupWithImageUrl (imageUrl: String) {
self.contentMode = .scaleAspectFill
self.clipsToBounds = true
self.layer.cornerRadius = 14
self.layer.borderWidth = 0
lastImageUrl = imageUrl
if let cachedImage = imageCache[imageUrl] {
self.image = cachedImage
return
}
guard let url = URL(string: imageUrl) else {
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error {
print(error)
return
}
guard let data = data else {
return
}
guard let image = UIImage(data : data) else {
return
}
imageCache[url.absoluteString] = image
}
if (url.absoluteString != self.lastImageUrl) {
return
}
DispatchQueue.main.async {
self.image = self.image
}
}
}
為什么“通過這種方式”不能確保在圖像視圖中加載了正確的圖像。 我閱讀了您的代碼,我認為您的代碼適合collectionViewController cellForItemAt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.