图片网址没有缓存

时间:2018-02-05 05:55:34

标签: ios swift alamofire image-caching alamofireimage

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

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

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

 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
}

}

我很困惑,问题是什么。是缩放问题还是网址问题。它是服务器端问题还是客户端问题。

1 个答案:

答案 0 :(得分:0)

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