I'm using the following code to display images in a uiimage. When i use an url for an image that has the file extension it works fine.
Example image url that works: https://mysite23423.com/image.jpg
But when I use an image that my server serves thats missing the file extension in the url it doesnt display it.
Example image url that doesnt works: https://mysite23423.com/image
How can I correct this issue?
func loadImage(urlString: String) {
lastURLUsedToLoadImage = urlString
image = nil
if let cachedImage = imageCache[urlString] {
image = cachedImage
return
}
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
if let err = err {
print("Failed to fetch post image:", err)
return
}
if url.absoluteString != self.lastURLUsedToLoadImage { return }
guard let imageData = data else { return }
let photoImage = UIImage(data: imageData)
imageCache[url.absoluteString] = photoImage
DispatchQueue.main.async {
self.image = photoImage
}
}.resume()
}