Comments (5)
这是我的使用代码
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.
我在 stack overflow 上同样在跟进此问题: https://stackoverflow.com/questions/78676826/how-to-optimize-or-release-excessive-dirty-memory-caused-by-imageio-in-ios
from kingfisher.
建议可以监测一下在下面的情况下这部分内存能不能被释放:
- 建议检查和确保相关的图片不再有强引用 (比如 image view 已经销毁)
- 调用
clearMemoryCache
- 触发一次 memory warning
from kingfisher.
- KF 内部有监听 memory warning 通知,会执行 clearMemoryCache, 但是似乎在内存危险水位无法触发或者无法及时触发,
我尝试设置了以下优化措施, 任然无法彻底解决OOM 的问题, 我有几个预想的方案: - totalCostLimit设置的更小, 比如 crashAmountMemory * 0.15
- 定时去清理 clearMemoryCache
- 或者在一些功能内敛的模块,如直播,在退出时候固定清理这块图片内存。
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.
如果能够通过 clearMemoryCache
释放的话,说明这部分内存的申请和持有都没有问题。除了 memory warning 触发(这个只能选择信任OS会在合适的时候给出)的 NSCache
的清理外,Kingfisher 自身也监听了切后台的通知,会在app切换到后台时清理掉这部分内存缓存。
设置更小的内存 cache size 以及手动去更多调用 clearMemoryCache
也都是可以采用的方法。
from kingfisher.
Related Issues (20)
- Crash at ImageContext.swift line 84. EXC_BAD_ACCESS (SIGSEGV) HOT 1
- Race Condition in ImageDownloader.addDownloadTask HOT 1
- Some Crashs On iPad5.3
- CPListItem' is only available in iOS 12.0 or newer HOT 1
- 同一个cacheKey有不用的downloadURL,主要是加入size控制,如何实现同一cacheKey只缓存size更大的图片数据呢
- [Question] Show a stub ONLY after attempt to load image by URL. HOT 1
- SwiftUI中如何使用progressiveJPEG
- Memory issue
- How do I add suffixes to cache paths, such as.jpg HOT 2
- Undefined symbols for architecture arm64: HOT 1
- ingfisherWebP 在iOS16.7.7 系统中,加载webp 图片不会动,在其他系统中没问题 HOT 1
- Disk Cache can grow significantly larger than 'sizeLimit' HOT 4
- dyld[769]: Library not loaded: @rpath/lib/libswiftCoreGraphics.dylib HOT 1
- No trust evaluation handling after receiving the authentication challenge HOT 1
- No exact matches in call to instance method 'retrieveImage' HOT 1
- 使用ResizingImageProcessor Crash HOT 7
- SessionDelegate.urlSession(_:dataTask:didReceive:) HOT 2
- Unable to cancel task if an image has a custom Image Processor HOT 1
- Need help "converting" .border(.primary) from SwiftUI to UIKit :O
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kingfisher.