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

标签 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

当我尝试检索缓存图像时打印它:URL 是什么? : http://www.somelink.com/DT199.jpg 我们实际返回的是:nil

我不知道为什么会这样,我之前试过几次,效果很好。我也读到了这些函数,它们不返回任何 bool 值响应,通过它我们可以知道图像是否在缓存中。

后来,我尝试使用下载管理器也没有任何反应,仍然毫 headless 绪。如果有人可以帮助我?这是代码

 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))")

                    }


                }


            })


        }




        }
        }

最佳答案

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

根据您的代码示例,它应该如下所示:

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

关于ios - 为什么我的图像没有缓存在 AlamofireImage 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37536195/

相关文章:

ios - 使用相同的 Bundle ID、应用程序名称等重新创建 iOS 应用程序

ios - Podspec 验证但未安装文件

swift - UISplitViewController 检测后退按钮按下

php - 如何将 base64 导出的公钥转换为我可以在 php 中使用的内容?

swift - Alamofire 内存泄漏仪器

ios - 如何改进 xcode 4.5 调试器中作为对象的变量的显示

ios - ABAddressBookGetAuthorizationStatus 在设置中授权后被拒绝

ios - 当我的应用程序关闭时,解析服务器实时查询确实收到了新对象吗?

ios - 从alamofire获取数据(responseString)

ios - Alamofire 概括请求