I have a problem I hope you can help solve. I am using Kingfisher in my app, and it is wonderful. In my main user search tableview, it loads and caches images perfectly. But oddly when I try to cache images in another tableview, where the images are larger width and height size, it does not load them. Weirdly it only loads one image or two only usually. Now I thought this had to do with the size of the image, but I don't really thing so. Here is some code to show I am download and storing the image in 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)
}
})
}
A now for how I am loading/fetching the urls into an array. The height and width of the 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)
}
And lastly the caching the image
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
}
So to sum it up, some images load, while others don't though I download and store them the same way. I would like to find a solution to this, so if you have one, please respond. I really appreciate it.
Try to cache them in a folder created in Documents like this
1- put this code in 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
}
Probably one of the stupidest mistakes I have ever made in programming. I accidentally copied an pasted the code for saving a profile image to saving multiple images. Well in order to store those images and reference them, they need different keys. If you look at my code above, they are not individual, this is why it kept only downloading one image. Solved it by doing
let storage = Storage.storage().reference().child(uid).child("userPhotos").child("photo\(key)")
3 hours wasted, whatever lol
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.