简体   繁体   English

翠鸟图像加载错误

[英]Kingfisher image loading error

I have a problem I hope you can help solve. 我有一个问题,希望您能解决。 I am using Kingfisher in my app, and it is wonderful. 我在我的应用程序中使用Kingfisher,这很棒。 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 这是一些代码,显示我正在下载图像并将其存储在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 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 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- 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 浪费3小时,不管哈哈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM