【问题标题】:Image Url not caching图片网址未缓存
【发布时间】:2018-02-05 05:55:33
【问题描述】:

我已经从我的应用程序上传了图像,当我想下载它时无法缓存。

我的网址:https://workmate.mapmyindia.com/app/mobile/displayImage/3b9c509d-6461-455d-8a2a-461adc73b81e

如果我用其他 url 替换字符串,它可以使用以下代码。

 var imageUrl : String = "https://workmate.mapmyindia.com/app/mobile/displayImage/3b9c509d-6461-455d-8a2a-461adc73b81e"

        if let image = WMRequestManager().cachedImage(for: imageUrl) {
            cell.taskImageView.image = image

        }
        else {
            WMRequestManager().downloadImage(imageURL: imageUrl, success: { (img) in
                cell.taskImageView.image = img
            })
        }



  //    MARK:- Image Download
func downloadImage(imageURL: String, success: @escaping (_ response:UIImage?) -> Void) {
    let urlString = imageURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
    Alamofire.request(urlString!, method: .get).responseImage { response in
        guard let image = response.result.value else {
            // Handle error
            success(nil)
            return
        }
        self.cache(image, for: urlString!)
        success(image)
    }
}
//MARK: - Image Caching

let imageCache = AutoPurgingImageCache(
    memoryCapacity: UInt64(100).megabytes(),
    preferredMemoryUsageAfterPurge: UInt64(60).megabytes()
)

func cache(_ image: Image, for url: String) {
    imageCache.add(image, withIdentifier: url)
}

func cachedImage(for url: String) -> Image? {
    return imageCache.image(withIdentifier: url)
}


extension UInt64 {

func megabytes() -> UInt64 {
    return self * 1024 * 1024
}

}

我被困住了,这会是什么问题。是缩放问题还是 url 问题。是服务器端问题还是客户端问题。

【问题讨论】:

    标签: ios swift alamofire image-caching alamofireimage


    【解决方案1】:

    如果您可以尝试使用为您缓存图像的KingFisher(或类似的东西)。从长远来看,它会让你的生活更轻松,并且会避免你的整个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 2021-07-09
      相关资源
      最近更新 更多