为什么我的图像没有被缓存在AlamofireImage中?

时间:2016-05-31 03:04:09

标签: ios swift alamofire tvos alamofireimage

此刻我很无能为力,我不知道为什么每次我想要检索时都会得到nil。我正在传递正确的图像数据和标识符。这是一个小代码

 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我们实际返回的是:nil

我不知道为什么会发生这种情况我以前曾尝试过几次并且效果很好。我还读到了这些函数,它们没有返回任何布尔响应,通过它我们可以知道图像是否在缓存中。

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

 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 个答案:

答案 0 :(得分:1)

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

依赖于您的代码示例,它应如下所示:

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