没有响应或跳过完成块 AlamofireImage
No response or Completion Block Skipped AlamofireImage
所以我在尝试让 AlamofireImages ImageDownloader 工作时遇到了一些麻烦。下面是我试图用来下载和稍后缓存图像的函数。
我以前使用普通的 Alamofire 来请求图像,但是,该项目已经发展到包括将图像缓存到磁盘以减少对服务器的请求量。
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)
}
}
}
}
查看 AlamofireImage 文档 here,发送下载请求看起来非常简单,我觉得我应该至少收到一条失败消息。完成块只是一起被跳过,我不完全确定为什么,除非它与此有关,这也在文档中。
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.
这可能是一个有点愚蠢的问题,但我不是在执行上述操作导致未调用闭包吗?
如果有人有一些见解,那就太棒了。
编辑:包括对 ImageDownloader 的强烈引用。
编辑#2:将下载分配给变量并打印响应代码 returns 为零。 (也许是因为请求是在不同的线程上完成的?)
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
}
编辑#3:
我终于修复了它,我的代码现在看起来像这样:
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)
}
}
}
}
我真正改变的是:
let downloader = ImageDownloader()
收件人:
let downloader = ImageDownloader.default
我真的希望这能帮助遇到这个问题的任何人!
你需要:
class MyClass
{
var downloader: ImageDownloader? // This acts as the 'strong reference'.
...
func downloadAndCacheImage(_ url: String, imageView: UIImageView)
{
self.downloader = ImageDownloader()
...
}
}
因为下载发生在您 downloadAndCacheImage()
returns 之后(使用其他线程)。
您当前的 downloader
是 downloadAndCacheImage()
的 local/automatic 变量。 downloadAndCacheImage()
结束时释放。这过早地 stops/cuts 下载过程。
我通过将代码更改为以下内容设法解决了我的问题。由于某种原因,下面的行更改修复了它。
让下载器 = ImageDownloader()
至
让下载器=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)
}
}
}
}
希望这对遇到它的人有所帮助!
所以我在尝试让 AlamofireImages ImageDownloader 工作时遇到了一些麻烦。下面是我试图用来下载和稍后缓存图像的函数。
我以前使用普通的 Alamofire 来请求图像,但是,该项目已经发展到包括将图像缓存到磁盘以减少对服务器的请求量。
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)
}
}
}
}
查看 AlamofireImage 文档 here,发送下载请求看起来非常简单,我觉得我应该至少收到一条失败消息。完成块只是一起被跳过,我不完全确定为什么,除非它与此有关,这也在文档中。
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.
这可能是一个有点愚蠢的问题,但我不是在执行上述操作导致未调用闭包吗?
如果有人有一些见解,那就太棒了。
编辑:包括对 ImageDownloader 的强烈引用。
编辑#2:将下载分配给变量并打印响应代码 returns 为零。 (也许是因为请求是在不同的线程上完成的?)
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
}
编辑#3: 我终于修复了它,我的代码现在看起来像这样:
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)
}
}
}
}
我真正改变的是:
let downloader = ImageDownloader()
收件人:
let downloader = ImageDownloader.default
我真的希望这能帮助遇到这个问题的任何人!
你需要:
class MyClass
{
var downloader: ImageDownloader? // This acts as the 'strong reference'.
...
func downloadAndCacheImage(_ url: String, imageView: UIImageView)
{
self.downloader = ImageDownloader()
...
}
}
因为下载发生在您 downloadAndCacheImage()
returns 之后(使用其他线程)。
您当前的 downloader
是 downloadAndCacheImage()
的 local/automatic 变量。 downloadAndCacheImage()
结束时释放。这过早地 stops/cuts 下载过程。
我通过将代码更改为以下内容设法解决了我的问题。由于某种原因,下面的行更改修复了它。
让下载器 = ImageDownloader()
至
让下载器=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)
}
}
}
}
希望这对遇到它的人有所帮助!