簡體   English   中英

翠鳥圖像加載錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM