ios - No response or Completion Block Skipped AlamofireImage -
so i'm having little bit of trouble trying alamofireimages imagedownloader work. below function i'm trying use download , later cache image.
i've used normal alamofire request images before, however, project has evolved include caching image disk reduce amount of requests 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 alamofireimage documentation here, looks pretty simple send download request , feel though have should getting @ least failure message. completion block getting skipped , i'm not entirely sure why unless has in documentation.
make sure keep strong reference imagedownloader instance, otherwise completion closure not called because downloader reference go out of scope before completion closure can called.
this might bit of dumb question, not doing above resulting in closure not being called?
if has insight, awesome.
edit: included strong reference imagedownloader.
edit#2: assigning download variable , printing response code returns nil. (maybe because request done on 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: fixed , code looks 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 changed was:
let downloader = imagedownloader()
to:
let downloader = imagedownloader.default
i hope helps comes across problem!
you need:
class myclass { var downloader: imagedownloader? // acts 'strong reference'. ... func downloadandcacheimage(_ url: string, imageview: uiimageview) { self.downloader = imagedownloader() ... } }
because download happens after downloadandcacheimage()
returns (using other thread(s)).
your current downloader
local/automatic variable of downloadandcacheimage()
. released when downloadandcacheimage()
ends. prematurely stops/cuts download process.
Comments
Post a Comment