解决单个图片解码后内存占用超过内存缓存最大值,导致无法显示图片的问题

Signed-off-by: madixin <madixin@huawei.com>
This commit is contained in:
madixin 2024-11-06 15:50:15 +08:00
parent b0661e83ac
commit 61d475921f
3 changed files with 21 additions and 13 deletions

View File

@ -1,3 +1,7 @@
## 3.2.0-rc.3
- The image cannot be displayed when decoding a single image more memory than the maximum value of the memory cache
## 3.2.0-rc.2
- Added callback information for image loading
- Added the interface for obtaining the upper limit and size of the current cache and the number of images corresponding to the current cache

View File

@ -14,7 +14,7 @@
"main": "index.ets",
"repository": "https://gitee.com/openharmony-tpc/ImageKnife",
"type": "module",
"version": "3.2.0-rc.2",
"version": "3.2.0-rc.3",
"dependencies": {
"@ohos/gpu_transform": "^1.0.2"
},

View File

@ -46,6 +46,11 @@ export class MemoryLruCache implements IMemoryCache {
throw new Error('key or value is invalid ');
}
let size = this.getImageKnifeDataSize(value)
if (size <= 0 && size >= this.maxMemory) {
return
}
// 如果size满了的话需要按照LRU的方式删除第一个
if (this.lruCache.length == this.maxSize && !this.lruCache.contains(key)) {
this.remove(this.lruCache.keys()[0])
@ -53,12 +58,9 @@ export class MemoryLruCache implements IMemoryCache {
this.remove(key)
}
let pre: ImageKnifeData = this.lruCache.put(key, value)
this.addMemorySize(value)
// if (pre !== undefined) { // 当前返回不是key的之前value
// this.removeMemorySize(pre)
// }
this.trimToSize();
this.lruCache.put(key, value)
this.currentMemory += size
this.trimToSize()
}
get(key: string): ImageKnifeData | undefined {
@ -119,19 +121,21 @@ export class MemoryLruCache implements IMemoryCache {
}
}
private addMemorySize(value: ImageKnifeData): void {
private getImageKnifeDataSize(value: ImageKnifeData): number {
if (value.source != undefined) {
if (typeof value.source === 'string' && value.source != "") {
this.currentMemory += value.source.length
return value.source.length
} else if (value.source == "") {
for (let index = 0;index < value.imageAnimator!.length;index++) {
let size: number = 0
for (let index = 0; index < value.imageAnimator!.length; index++) {
let pixelMap = value.imageAnimator![index].src as PixelMap
this.currentMemory += pixelMap.getPixelBytesNumber()
size += pixelMap.getPixelBytesNumber()
}
return size
} else {
this.currentMemory += value.source.getPixelBytesNumber();
return value.source.getPixelBytesNumber();
}
//LogUtil.log("MemoryCache addMemorySize: " + value.source.getPixelBytesNumber() + " currentMemory" + this.currentMemory)
}
return 0
}
}