I am trying to download an image from this url:
https://i.ebayimg.com/00/s/MTA2NlgxNjAw/z/TaoAAOSwjkdZ57Jm/$_2.jpg
Using this code:
guard let escapedQuery = rawUrl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed),
let url = URL(string: escapedQuery) else { return }
// check if image is new image, if not, stick with the old image
if(previousUrl != rawUrl){
previousUrl = rawUrl
showActivityIndicatory()
print("Download Started")
// download the image from the provided url
getDataFromUrl(url: url) { data, response, error in
guard let data = data, error == nil else {
DispatchQueue.main.async() {
// stop the animation/loading icon
self.imageLoadingIndicator.stopAnimating()
// set a default placeholder image
self.image = #imageLiteral(resourceName: "CellDefaultImage")
}
return
}
print(response?.suggestedFilename ?? url.lastPathComponent)
print("Download Finished")
DispatchQueue.main.async() {
self.image = UIImage(data: data)
self.imageLoadingIndicator.stopAnimating()
}
}
}
}
func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
// try to download the raw data on a different thread to provide optimal perfomance regarding UI
URLSession.shared.dataTask(with: url) { data, response, error in
completion(data, response, error)
}.resume()
}
func showActivityIndicatory() {
imageLoadingIndicator.center = self.center
imageLoadingIndicator.startAnimating()
}
But I am constantly running into : HTTP load failed (error code: -1200 [3:-9802]) or error code -1002 etc.
Could someone point out what is wrong, perhaps my image url formatting is wrong?