لقد حاولت تحميل الصور بشكل غير متزامن للهدوء لبعض الوقت الآن. عندما أقوم بإنشاء فصل جديد لهذا الأمر وقمت بإنشاء وتشغيل التطبيق ، يظهر خطأ في سجل وقت التشغيل
// 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
}
}
}