[英]Kingfisher image loading error
我有一个问题,希望您能解决。 我在我的应用程序中使用Kingfisher,这很棒。 在我的主要用户搜索表视图中,它完美地加载和缓存了图像。 但是奇怪的是,当我尝试将图像缓存在另一个较大的宽度和高度尺寸的表格视图中时,它不会加载它们。 奇怪的是,它通常只加载一两个图像。 现在我认为这与图像的大小有关,但我并不是真的。 这是一些代码,显示我正在下载图像并将其存储在Firebase中
let storage = Storage.storage().reference().child(uid).child("userPhoto.jpg")
if let uploadData = UIImageJPEGRepresentation(photo, 0.2) {
storage.putData(uploadData, metadata: nil, completion:
{ (metadata, error) in
if error != nil {
print(error!)
return
}
if let imagerUrl = metadata?.downloadURL()?.absoluteString {
let ref = Database.database().reference()
let randomString = NSUUID().uuidString
print(randomString)
let postFeed = [randomString : imagerUrl]
print(self.photos)
self.collectionerView.reloadData()
self.activityer.stopAnimating()
ref.child("users").child(uid).child("profilePhotos").updateChildValues(postFeed)
}
})
}
现在介绍如何将网址加载/提取到数组中。 imageView的高度和宽度= 150 * 150
func grabPhotos (uid: String) {
let ref = Database.database().reference()
ref.child("users").child(uid).child("profilePhotos").observeSingleEvent(of: .value, with: {(snapshot) in
if let theirPhotos = snapshot.value as? [String : AnyObject] {
for (_,one) in theirPhotos {
let newPhoto = Photo()
newPhoto.photoUrl = one as? String
print(one as! String)
self.imagers.append(newPhoto)
}
self.tablerView.reloadData()
} else {
self.activityer.stopAnimating()
}
}, withCancel: nil)
}
最后是缓存图像
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tablerView.dequeueReusableCell(withIdentifier: "userImagers", for: indexPath) as! UsersImagersTableViewCell
if let imagerl = self.imagers[indexPath.row].photoUrl {
let urler = URL(string: imagerl)
cell.imagerView.kf.setImage(with: urler)
} else {
cell.imagerView.image = UIImage(named: "profiler")
}
return cell
}
因此,总的来说,有些图像会加载,而另一些图像则不会,尽管我以相同的方式下载和存储它们。 我想找到一种解决方案,因此,如果有,请答复。 我真的很感激。
尝试将它们缓存在像这样的文档中创建的文件夹中
1-将此代码放入viewDidLoad
documentsUrl = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
var logsPath = documentsUrl.appendingPathComponent("Logs")
do {
try FileManager.default.createDirectory(at: logsPath!, withIntermediateDirectories: true, attributes: nil)
var res = URLResourceValues()
res.isExcludedFromBackup = true
try logsPath?.setResourceValues(res)
print("Created")
} catch let error as NSError {
NSLog("failed100 \(error.debugDescription)")
}
2
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = areaSettTable.dequeueReusableCell(withIdentifier:CellIdentifier1) as! logTableViewCell
let tr = allTrips[indexPath.row]
let imgPath = documentsUrl.appendingPathComponent("\(tr.tripId).jpg")
print("qwqwqw \((imgPath?.path)!)")
if(FileManager.default.fileExists(atPath:(imgPath?.path)!))
{
do
{
if let data:Data? = try Data.init(contentsOf:imgPath!)
{
cell.mapImageV.image=UIImage.init(data:data!)
cell.imgActivity.isHidden = true
}
} catch let error as NSError {
NSLog("failed1150 \(error.debugDescription)")
}
}
else
{
if(tr.img != "nil" && tr.img != "")
{
cell.imgActivity.isHidden = false
cell.imgActivity.startAnimating()
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
let url = NSURL(string:tr.img) // myurl is the webpage address.
let data = NSData(contentsOf:url! as URL)
let imgIn = documentsUrl.appendingPathComponent("\(tr.tripId).jpg")
DispatchQueue.main.async
{
print("This is run on the main queue, after the previous code in outer block")
if !( data == nil )
{
data?.write(to:imgIn!, atomically: true)
self.areaSettTable.reloadData()
}
else
{
print("nillll data . . .")
}
}
}
}
else
{
cell.imgActivity.isHidden = true
}
}
return cell
}
可能是我在编程中犯过的最愚蠢的错误之一。 我不小心将粘贴的用于保存个人资料图片的代码复制到保存多张图片。 为了存储这些图像并参考它们,它们需要不同的键。 如果您在上面查看我的代码,它们不是单独的,这就是为什么它仅下载一张图像的原因。 通过解决它
let storage = Storage.storage().reference().child(uid).child("userPhotos").child("photo\(key)")
浪费3小时,不管哈哈
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.