图像在缓存中占用的空间比tvos中的原始大小要多

MQ.*_*MQ. 1 caching swift swift2 tvos alamofireimage

Idk为什么我的图像在缓存中占用的尺寸比原始尺寸大.我正在使用alamofireImage库来实现此目的,我的应用程序是针对tvOS的.这是我的代码.

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


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

                if let JSON = response.result.value {
                    self.imagesArray = JSON["results"] as! NSMutableArray
                    for result in self.imagesArray{

                        self.getNetworkImage(result["url"] as! String, completion: { (UIImage) in

                        })}}}



func getNetworkImage(urlString: String, completion: (UIImage -> Void)) -> (Request) {
        return Alamofire.request(.GET, urlString, headers: headers).responseImage { (response) -> Void in
            guard let image = response.result.value else { return }
            completion(image)
            if response.response?.statusCode == 200{
                self.cacheImage(image, urlString: urlString)
                self.cachedImagesOnly.addObject(urlString)
            }
            if self.counter == 4{
               self.activityIndicatorView.stopAnimating()
               self.activityIndicatorView.hidden = true

                var downloadedImage = UIImage()
                let dicr = self.cachedImagesOnly.firstObject
                 let urlStringFetch = dicr!["url"] as! String
                print("Fetching url: \(urlStringFetch)")

                downloadedImage = self.cachedImage(urlStringFetch)!
                print("Size of image from cache: \(downloadedImage.size)")
                self.ssImage.image = downloadedImage

                })

            }

        }
    }

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

        print("Total Cache memory size: \(self.photoCache.memoryCapacity)")
        self.counter += 1
        let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
        print("Image size before cache : \(jpgImageData?.length)")
        self.cachedImagesOnly.addObject(urlString)
        let URLRequest = NSURLRequest(URL: NSURL(string: "\(urlString)")!)
        self.photoCache.addImage(UIImage(data: jpgImageData!)!, forRequest: URLRequest)
        print("Cache memory usage after image cache: \(self.photoCache.memoryUsage)")

    }
Run Code Online (Sandbox Code Playgroud)

记录结果:

Total Cache memory size: 104857600
Image size: (1752.0, 1896.0) and string as identifier: http://image.com/9493.jpg
Image size before cache : Optional(1738247)
Cache memory usage after image cache: 13287168
Total Cache memory size: 104857600
Image size: (2875.0, 3872.0) and string as identifier: http://image.com/5025.jpg
Image size before cache : Optional(7049508)
Cache memory usage after image cache: 57815168
Total Cache memory size: 104857600
Image size: (2394.0, 3866.0) and string as identifier: http://image.com/169215.jpg
Image size before cache : Optional(6049349)
Cache memory usage after image cache: 94835984
Total Cache memory size: 104857600
Image size: (3811.0, 3049.0) and string as identifier: http://image.com/786.jpg
Image size before cache : Optional(2848557)
Cache memory usage after image cache: 46478956
Run Code Online (Sandbox Code Playgroud)

因此,我在GitHub上读取它们在缓存内存已满时使用FIFO,所以除了删除第一个对象外,它根据计算删除了两个或三个.而且,只考虑第一个日志结果,图像大小是1738247和IDK这是什么开销,它在缓存后使用大小变为13287168几乎是非常奇怪的13倍.如果有人遇到这个问题或者能够识别出我的错误,将会非常感激.提前致谢.

Rob*_*Rob 5

这有点复杂,因为这里有很多活动部件:

  • 首先,当您从Web服务获取图像时,会有原始资产,a NSData.在你的代码片段中,我认为你没有看到这个原始资产,因为你让Alamofire UIImage在交付给你之前将它转换为(见下一点).但是如果您在网络浏览器(或者像Charles或WireShark这样的工具)中观看此内容,则这是原始资产的大小.

  • 然后将其转换为a UIImage,如果原始资产被压缩,则可以保持压缩,直到您首次在UIImageView等等中使用它.

    一旦你使用它UIImage,它就会被解压缩,通常需要更多的内存,通常是每个像素四个字节(红色,绿色,蓝色和alpha通道分别为一个字节,但也有其他格式).

  • 当你以后打电话时UIImageJPEGRepresentation,你正在构建一个新NSDataUIImage.人们经常错误地认为这与原始资产相同,但事实并非如此.如果你使用1.0的压缩质量,这通常比原始资产大得多(虽然可能小于未压缩的UIImage.)如果你使用较低的压缩质量,大小通常更合理,但你引入了一些JPEG工件,因为它有损压缩.(PNG压缩是无损的,但通常产生的资产更大.)

因此,您将NSData表示(特别compressionQuality是1的JPEG)与UIImage缓存的内存量进行比较的长期和短期,我不希望这些数字匹配.他们是非常不同的东西.

  • 在我必须实现自己的缓存的情况下,我(a)缓存原始资产,而不是"UIImage"以避免缓存未压缩的图像; (b)使用`NSCache`(我设置为观察`UIApplicationDidReceiveMemoryWarningNotification`,在内存压力下清除自身)以及`NSSearchPathDirectory.CachesDirectory`中的持久存储来编写缓存到内存的缓存代码,基本上首先尝试`NSCache`,如果`NSCache`失败,则回退到持久存储. (2认同)