From 2a81312ed5f31c1c2ea0b3b5c22a3487168a08b8 Mon Sep 17 00:00:00 2001 From: tyBrave Date: Sat, 12 Oct 2024 09:57:11 +0800 Subject: [PATCH] add get cache data Signed-off-by: tyBrave --- entry/src/main/ets/pages/Index.ets | 5 + .../src/main/ets/pages/TestCacheDataPage.ets | 100 ++++++++++++++++++ .../resources/base/profile/main_pages.json | 3 +- library/src/main/ets/ImageKnife.ets | 61 +++++++++++ 4 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 entry/src/main/ets/pages/TestCacheDataPage.ets diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 75fa2f2..6cbb7a6 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -159,6 +159,11 @@ struct Index { uri: 'pages/TestTaskResourcePage', }); }) + Button("测试缓存数据").margin({top:10}).onClick(()=>{ + router.push({ + uri: 'pages/TestCacheDataPage', + }); + }) } } .width('100%') .height('100%') diff --git a/entry/src/main/ets/pages/TestCacheDataPage.ets b/entry/src/main/ets/pages/TestCacheDataPage.ets new file mode 100644 index 0000000..3cdb530 --- /dev/null +++ b/entry/src/main/ets/pages/TestCacheDataPage.ets @@ -0,0 +1,100 @@ +import { ImageKnife, CacheStrategy, ImageKnifeComponent, ImageKnifeOption } from '@ohos/imageknife'; + +@Entry +@ComponentV2 +struct TestCacheDataPage { + @Local cacheUpLimit: string = '当前缓存上限: 0M'; + @Local currentNum: string = '当前缓存图片数量: 0'; + @Local currentSize: string = '当前缓存的大小: 0M'; + @Local currentWidth: number = 200 + @Local currentHeight: number = 200 + @Local ImageKnifeOption: ImageKnifeOption = new ImageKnifeOption({ + loadSrc: "", + objectFit: ImageFit.Contain, + onLoadListener: { + onLoadFailed: (err) => { + console.error("Load Failed Reason: " + err); + }, + onLoadSuccess: (data) => { + return data; + }, + }, + border: { radius: 50 } + }) + + aboutToAppear(): void { + ImageKnife.getInstance().initFileCache(getContext(this), 256, 256 * 1024 * 1024,"ImageKnifeCache1") + } + + build() { + Column() { + + ImageKnifeComponent( + { imageKnifeOption: this.ImageKnifeOption }) + .height(this.currentHeight) + .width(this.currentWidth) + .margin({ top: 10 }) + + Button('内存加载图片') + .onClick(() => { + this.ImageKnifeOption = new ImageKnifeOption({ + loadSrc: "https://img0.baidu.com/it/u=1530797181,174436037&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800", + objectFit: ImageFit.Contain, + writeCacheStrategy:CacheStrategy.Memory, + border: { radius: 50 }, + }) + }) + Button('磁盘缓存加载图片') + .onClick(() => { + this.ImageKnifeOption = new ImageKnifeOption({ + loadSrc: "https://q7.itc.cn/images01/20240223/ce80229bf9934dff97cdf2ad7be1dcb8.jpeg", + objectFit: ImageFit.Contain, + writeCacheStrategy:CacheStrategy.File, + border: { radius: 50 }, + }) + }) + Text(this.cacheUpLimit).fontSize(20).margin({bottom:8}); + Text(this.currentNum).fontSize(20).margin({bottom:8}); + Text(this.currentSize).fontSize(20).margin({bottom:20}); + + Button("获取当前内存缓存上限").onClick(() => { + let result = ImageKnife.getInstance().getCacheUpperLimit(CacheStrategy.Memory); + if (result) { + this.cacheUpLimit = "当前缓存上限:" + (result/(1024*1024))+"M"; + } + }).margin({bottom:8}); + Button("获取当前内存缓存图片数量").onClick(() => { + let result = ImageKnife.getInstance().getCurrentPicturesNum(CacheStrategy.Memory); + if (result) { + this.currentNum = "当前缓存图片数量:" + result; + } + }).margin({bottom:8}); + Button("获取当前缓存的大小").onClick(() => { + let result = ImageKnife.getInstance().getCurrentCacheSize(CacheStrategy.Memory); + if (result) { + this.currentSize = "当前缓存的大小:" + (result/(1024*1024))+"M"; + } + }).margin({bottom:8}); + + Button("获取当前磁盘缓存上限").onClick(() => { + let result = ImageKnife.getInstance().getCacheUpperLimit(CacheStrategy.File); + if (result) { + this.cacheUpLimit = "当前缓存上限:" + (result/(1024*1024))+"M"; + } + }).margin({bottom:8}); + Button("获取当前磁盘缓存图片数量").onClick(() => { + let result = ImageKnife.getInstance().getCurrentPicturesNum(CacheStrategy.File); + if (result) { + this.currentNum = "当前缓存图片数量:" + result; + } + }).margin({bottom:8}); + Button("获取当前磁盘的大小").onClick(() => { + let result = ImageKnife.getInstance().getCurrentCacheSize(CacheStrategy.File); + if (result) { + this.currentSize = "当前缓存的大小:" + (result/(1024*1024))+"M"; + } + }).margin({bottom:8}); + } + .height('100%').width('100%').margin({top:50}) + } +} \ No newline at end of file diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index d42cd2d..7b16f4e 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -22,6 +22,7 @@ "pages/ImageAnimatorPage", "pages/TestSetCustomImagePage", "pages/TestErrorHolderPage", - "pages/TestTaskResourcePage" + "pages/TestTaskResourcePage", + "pages/TestCacheDataPage" ] } \ No newline at end of file diff --git a/library/src/main/ets/ImageKnife.ets b/library/src/main/ets/ImageKnife.ets index 41eeb64..e91344a 100644 --- a/library/src/main/ets/ImageKnife.ets +++ b/library/src/main/ets/ImageKnife.ets @@ -302,6 +302,67 @@ export class ImageKnife { return this.fileCache as FileCache } + /** + * get cache upper limit + * @param cacheType + * @returns + */ + getCacheUpperLimit(cacheType?: CacheStrategy): number | undefined { + if (cacheType == undefined || cacheType == CacheStrategy.Default) { + cacheType = CacheStrategy.Memory; + } + if (cacheType == CacheStrategy.Memory) { + return (this.memoryCache as MemoryLruCache).maxMemory; + } else { + console.log("sss---->isFileCacheInit:"+this.fileCache) + if (this.isFileCacheInit()) { + return this.fileCache?.maxMemory; + } else { + throw new Error("the disk cache not init"); + } + } + } + + /** + * gets the number of images currently cached + * @param cacheType + * @returns + */ + getCurrentPicturesNum(cacheType: CacheStrategy): number | undefined { + if (cacheType == undefined || cacheType == CacheStrategy.Default) { + cacheType = CacheStrategy.Memory; + } + if (cacheType == CacheStrategy.Memory) { + return (this.memoryCache as MemoryLruCache).size(); + } else { + if (this.isFileCacheInit()) { + return this.fileCache?.size(); + } else { + throw new Error("the disk cache not init"); + } + } + } + + /** + * gets the current cache size + * @param cacheType + * @returns + */ + getCurrentCacheSize(cacheType: CacheStrategy): number | undefined { + if (cacheType == undefined || cacheType == CacheStrategy.Default) { + cacheType = CacheStrategy.Memory; + } + if (cacheType == CacheStrategy.Memory) { + return (this.memoryCache as MemoryLruCache).currentMemory; + } else { + if (this.isFileCacheInit()) { + return this.fileCache?.currentMemory; + } else { + throw new Error("the disk cache not init"); + } + } + } + private pixelMapToArrayBuffer(pixelMap: PixelMap): ArrayBuffer { let imageInfo = pixelMap.getImageInfoSync();