ios - 翠鸟图像加载错误

标签 ios swift caching kingfisher

我有一个问题希望你能帮忙解决。我在我的应用程序中使用 Kingfisher,它很棒。在我的主要用户搜索 TableView 中,它完美地加载和缓存图像。但奇怪的是,当我尝试在另一个 tableview 中缓存图像时,图像的宽度和高度尺寸更大,它不会加载它们。奇怪的是,它通常只加载一两张图片。现在我认为这与图像的大小有关,但我不这么认为。这是一些代码来显示我正在下载图像并将其存储在 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)
                        }
                })
            }

现在介绍我如何将 URL 加载/提取到数组中。 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
}

总而言之,有些图像会加载,而另一些则不会,尽管我以相同的方式下载和存储它们。我想找到一个解决方案,所以如果你有一个,请回复。对此,我真的非常感激。

最佳答案

尝试将它们缓存在像这样在 Documents 中创建的文件夹中

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



}

关于ios - 翠鸟图像加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47853017/

相关文章:

ios - 多个标签 xAxis iOS-Charts

php - 在 PHP 中缓存?

iOS动态加载自定义字体错误收集保留

swift - 为什么我有我的子类的方法?

ios - 如何在 SWIFT 3 中将 HTTPS 代理添加到 NSURLSession

javascript - 通过 $.get 加载外部文件通常不会返回响应,即使响应代码是 200

c# - 使用 memcached 缓存和检索数据列表

ios - 使用全局变量在 ViewController 之间传递数据有哪些缺点?

ios - 我可以将 p8 APNs Auth Key 转换为 pem 文件吗?

ios - 放大 Storyboard 中的任何 View 时 Xcode 8.2 和 8.1 崩溃