Giter Club home page Giter Club logo

Comments (5)

make1a avatar make1a commented on August 24, 2024

这是我的使用代码

extension UIImageView {
    
    @nonobjc
    /// 设置网络图片的统一入口方法
    /// - Parameters:
    ///   - source: 可以传入 String 或者 URL 类型, 必须
    ///   - placeholder: 占位图,  在列表上使用的时候, 推荐放一张图片作为占位使用
    ///   - size: 图片解码到内存之前,按照大小去解码图片,解决大图 OOM 的问题。在已知图片大小的情况下,强烈建议使用该参数
    public func kfSetImage(
        url source: Any?,
        placeholder: UIImage? = nil,
        size: CGSize = .zero,
        options: KingfisherOptionsInfo? = nil,
        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) {
            
            guard let source = source else {
                self.image = placeholder
                return
            }
            
            if #available(iOS 14, *) {} else if completionHandler == nil {
                setImageIos13(url: source, placeholder: placeholder)
                return
            }
            
            var defaultOptions: KingfisherOptionsInfo = [
                .scaleFactor(UIScreen.main.scale),
                .cacheOriginalImage
            ]
            
            if let options = options {
                defaultOptions += options
            }
            
            guard let url = self.url(from: source) else {
                self.image = placeholder
                return
            }
            
            setImage(with: url, placeholder: placeholder, size: size, options: &defaultOptions, completionHandler: completionHandler)
        }
    
    private func setImage(
        with url: URL,
        placeholder: UIImage?,
        size: CGSize,
        options: inout KingfisherOptionsInfo,
        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) {
            
            if #available(iOS 14, *) {
                
                /// autolayout 取不到 size
                /// 在已经获取到容器大小的情况下,强制开启ImageIO图片下取样
                var size = size
                if size == .zero {
                    size = self.bounds.size
                }
                
                // 获取合适大小的图片展示
                if !url.absoluteString.contains("gif"), size != CGSize.zero {
                    options += [.processor(DownsamplingImageProcessor(size: size))]
                }
            } else if url.absoluteString.contains("format,webp") {
                options += [.processor(WebPProcessor.default), .cacheSerializer(WebPSerializer.default)]
            }
            
            kf.setImage(with: url, placeholder: placeholder, options: options, completionHandler: completionHandler)
        }
}

from kingfisher.

make1a avatar make1a commented on August 24, 2024

我在 stack overflow 上同样在跟进此问题: https://stackoverflow.com/questions/78676826/how-to-optimize-or-release-excessive-dirty-memory-caused-by-imageio-in-ios

from kingfisher.

onevcat avatar onevcat commented on August 24, 2024

建议可以监测一下在下面的情况下这部分内存能不能被释放:

  1. 建议检查和确保相关的图片不再有强引用 (比如 image view 已经销毁)
  2. 调用 clearMemoryCache
  3. 触发一次 memory warning

from kingfisher.

make1a avatar make1a commented on August 24, 2024
  1. 可以确认是一定释放了的,反复做了很多测试。
  2. clearMemoryCache 得到了以下的结果,释放了大块内存:.
    image

image

  1. KF 内部有监听 memory warning 通知,会执行 clearMemoryCache, 但是似乎在内存危险水位无法触发或者无法及时触发,
    我尝试设置了以下优化措施, 任然无法彻底解决OOM 的问题, 我有几个预想的方案:
  2. totalCostLimit设置的更小, 比如 crashAmountMemory * 0.15
  3. 定时去清理 clearMemoryCache
  4. 或者在一些功能内敛的模块,如直播,在退出时候固定清理这块图片内存。
    public static func crashAmountMemory() -> Double {
        let name = UIDevice.name
        
        switch name {
        case .unknown:
            return Double(ProcessInfo.processInfo.physicalMemory) * 0.55

        case .iPad2: return Double(275) * 1024 * 1024
        case .iPad3: return Double(645) * 1024 * 1024
        case .iPad4: return Double(585) * 1024 * 1024
        case .iPadMini: return Double(297) * 1024 * 1024
        case .iPadAir: return Double(697) * 1024 * 1024
        case .iPadAir2: return Double(1383) * 1024 * 1024
        case .iPhone4: return Double(325) * 1024 * 1024
        case .iPhone4s: return Double(286) * 1024 * 1024
        case .iPhone5: return Double(645) * 1024 * 1024
        case .iPhone5s, .iPhone5c: return Double(646) * 1024 * 1024
        case .iPhone6: return Double(645) * 1024 * 1024
        case .iPhone6Plus: return Double(645) * 1024 * 1024
        case .iPhone6s: return Double(1396) * 1024 * 1024
        case .iPhone6sPlus: return Double(1392) * 1024 * 1024
        case .iPhoneSE: return Double(1395) * 1024 * 1024
        case .iPhone7: return Double(1395) * 1024 * 1024
        case .iPhone7Plus: return Double(2040) * 1024 * 1024
        case .iPhone8, .iPhone8Plus, .iPhoneXsMax: return Double(1364) * 1024 * 1024
        case .iPhoneX, .iPhoneXs, .iPhoneXr: return Double(1392) * 1024 * 1024
        case .iPhone11, .iPhone11Pro, .iPhone11ProMax: return Double(2068) * 1024 * 1024
        default: return Double(ProcessInfo.processInfo.physicalMemory) * 0.55
        }
    }

            ImageCache.default.memoryStorage.config.totalCostLimit = Int(DeviceChecker.crashAmountMemory() * 0.2)

from kingfisher.

onevcat avatar onevcat commented on August 24, 2024

如果能够通过 clearMemoryCache 释放的话,说明这部分内存的申请和持有都没有问题。除了 memory warning 触发(这个只能选择信任OS会在合适的时候给出)的 NSCache 的清理外,Kingfisher 自身也监听了切后台的通知,会在app切换到后台时清理掉这部分内存缓存。

设置更小的内存 cache size 以及手动去更多调用 clearMemoryCache 也都是可以采用的方法。

from kingfisher.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.