So I'm having a little bit of trouble trying to get AlamofireImages ImageDownloader to work. Below is the function I'm trying to use to download and later cache an image.
I've used normal Alamofire to request images before, however, the project has evolved to include caching an image to the disk to reduce the amount of requests to the server.
var downloader: ImageDownloader?
func downloadAndCacheImage(_ url: String, imageView: UIImageView)
{
self.downloader = ImageDownloader()
var urlRequest = try! URLRequest(url: "GiveMeAResponse.com", method: .get)
self.downloader?.download(urlRequest) { response in
switch response.result{
case .success(let image):
print("\(image) has been downloaded")
break
case .failure(let error):
print("ERROR: \(response.error)")
break
}
if let imageData = response.data{
debugPrint("Applying image...")
if let image = UIImage(data: imageData){
ImageManager.applyImage(imageView: imageView, image: image)
}
}
}
}
Looking through the AlamofireImage documentation here, it looks pretty simple to send a download request and I feel as though with what I have I should be getting at least a failure message. The completion block is just getting skipped all together and I'm not entirely sure why unless it has to do with this which is also in the documentation.
Make sure to keep a strong reference to the ImageDownloader instance, otherwise the completion closure will not be called because the downloader reference will go out of scope before the completion closure can be called.
This might be a bit of a dumb question, but am I not doing the above resulting in the closure not being called?
If anyone has some insight, that would be awesome.
Edit: Included strong reference to the ImageDownloader.
Edit#2: Assigning the download to a variable and printing the response code returns a nil. (Maybe because the request is done on a different thread?)
var downloader: ImageDownloader?
func downloadAndCacheImage(_ url: String, imageView: UIImageView)
{
self.downloader = ImageDownloader()
var urlRequest = try! URLRequest(url: URL(string: "https://solarianprogrammer.com/images/2013/02/28/mandelbrot_piece_Z2.png")!)
let downloadReciept = self.downloader?.download([urlRequest])
print(downloadReciept?[0].request.response) // Returns nil
}
Edit#3: I finally fixed it and my code now looks like this:
let downloader = ImageDownloader.default
func downloadAndCacheImage(_ url: String, imageView: UIImageView)
{
var urlRequest = try! URLRequest(url: URL(string: "https://solarianprogrammer.com/images/2013/02/28/mandelbrot_piece_Z2.png")!)
self.downloader.download(urlRequest) { response in
switch response.result{
case .success(let image):
print("\(image) has been downloaded")
break
case .failure(let error):
print("ERROR: \(response.error)")
break
}
if let imageData = response.data{
debugPrint("Applying image...")
if let image = UIImage(data: imageData){
ImageManager.applyImage(imageView: imageView, image: image)
}
}
}
}
All I really changed was:
let downloader = ImageDownloader()
To:
let downloader = ImageDownloader.default
I really hope this helps anyone who comes across this problem!
I managed to fix my problem by changing my code to the below. For some reason the below line change fixed it.
let downloader = ImageDownloader()
to
let downloader = ImageDownloader.default
let downloader = ImageDownloader.default
func downloadAndCacheImage(_ url: String, imageView: UIImageView)
{
var urlRequest = try! URLRequest(url: URL(string: "https://solarianprogrammer.com/images/2013/02/28/mandelbrot_piece_Z2.png")!)
self.downloader.download(urlRequest) { response in
switch response.result{
case .success(let image):
print("\(image) has been downloaded")
break
case .failure(let error):
print("ERROR: \(response.error)")
break
}
if let imageData = response.data{
debugPrint("Applying image...")
if let image = UIImage(data: imageData){
ImageManager.applyImage(imageView: imageView, image: image)
}
}
}
}
Hope this helps anyone who comes across it!