0

I have upload the image from my application and when I want to download its not able to cache.

My url : https://workmate.mapmyindia.com/app/mobile/displayImage/3b9c509d-6461-455d-8a2a-461adc73b81e

If I replace the string with other url, it works with following code.

 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
}

}

I am stuck with, what the issue it will be. Is it a scaling issue or url issue. Is it a server side issue or client side.

Myaaoonn
  • 1,001
  • 12
  • 25

1 Answers1

0

If you can try use KingFisher (or something similar) that does the caching of the image for you. It will make your life a lot easier in the long run and will avoid your whole problem.

kd02
  • 422
  • 5
  • 14