2016-05-31 58 views
2

我现在很无知,我不知道为什么每次我尝试检索时都会得到零。我传递正确的图像数据和标识符。这里有一个小代码为什么我的图像没有被缓存在AlamofireImage中?

let photoCache = AutoPurgingImageCache(
     memoryCapacity: 100 * 1024 * 1024, 
     preferredMemoryUsageAfterPurge: 60 * 1024 * 1024 
    ) 

func cacheImage(image: Image, urlString: String) { 

     print("Image size: \(image.size) and string as identifier: \(urlString)") 



     self.photoCache.addImage(image, withIdentifier: urlString) 
    } 

    func cachedImage(urlString: String) -> Image? { 

     print("What is url? : \(urlString)") 

     print("What we are actually returning? : \(self.photoCache.imageWithIdentifier(urlString))") 

     return self.photoCache.imageWithIdentifier(urlString) 
    } 

打印为cacheImage功能:图像尺寸:(2826.0,3722.0)和字符串作为标识符:HTTP:www.somelink.com/DT199.jpg

打印为cachedImage时我正在尝试检索它:URL是什么? :http:www.somelink.com/DT199.jpg我们实际返回的是:无

我不知道它为什么会发生我曾尝试过几次,它的工作完美。我还阅读了这些函数,他们不返回任何布尔响应,通过它我们可以知道图像是否在缓存中。

后来,我尝试使用下载管理器以及没有发生任何事情仍然无能为力。如果有人能帮助我?这里的代码

let imageDownloader = ImageDownloader(
      configuration: ImageDownloader.defaultURLSessionConfiguration(), 
      downloadPrioritization: .FIFO, 
      maximumActiveDownloads: 15, 
      imageCache: AutoPurgingImageCache() 
     ) 


     let downloader = imageDownloader 


     Alamofire.request(.GET, request, headers: headers) 
      .responseJSON { response in 

     if let JSON = response.result.value { 

     self.imagesArray = NSMutableArray() 
     self.imagesArray = JSON["results"] as! NSMutableArray 

      for results in self.imagesArray{ 

      let URLRequest = NSURLRequest(URL: NSURL(string:results["url"] as! String)!) 

      downloader.downloadImage(URLRequest: URLRequest, filter: nil, completion: { response in 

       if response.response?.statusCode == 200{ 

        print("In the house fetching url: \(results["url"] as! String)") 

        let image = response.result.value 
        print("Size of image: \(image?.size)") 
        let returnValue = downloader.imageCache?.addImage(image!, withIdentifier: results["url"] as! String) 
        print("return status: \(returnValue)") 

        if results .isEqual(self.imagesArray.lastObject){ 
         self.activityIndicatorView.stopAnimating() 
         self.activityIndicatorView.hidden = true 

         let fic = self.imagesArray.firstObject 
         print("getting image for this url : \(fic!["url"] as! String)") 

         var imageC = UIImage() 
         imageC = (downloader.imageCache?.imageWithIdentifier(fic!["url"] as! String))! 
         self.ssImage.image = imageC 
         print("Image : \(imageC.size))") 

        } 


       } 


      }) 


     } 




     } 
     } 

回答

1

也许你必须用你之前使用的photoCache来初始化你的imageDownloader以手动缓存图像。 AutoPurgingImageCache不是共享实例。只有UIImageView + AlamofireImage.swift扩展具有一个保留一个AutoPurgingImageCache的af_sharedImageDownloader。

在你的代码示例依赖它应该是这样的:

let imageDownloader = ImageDownloader(
      configuration: ImageDownloader.defaultURLSessionConfiguration(), 
      downloadPrioritization: .FIFO, 
      maximumActiveDownloads: 15, 
      imageCache: self.photoCache 
     ) 
相关问题