diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f17062..03a498c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 3.0.0-rc.8 +- svg解码单位改为px +- 修复预加载接口preLoadCache传ImageKnifeOption失效 +- 新增改变文件缓存目录接口reCacheFolder和设置文件缓存最大数接口reSize + ## 3.0.0-rc.7 - 修复成功回调获取不到宽高 - 新增svg图片解码 diff --git a/README.md b/README.md index 81b77b7..03955e5 100644 --- a/README.md +++ b/README.md @@ -310,6 +310,13 @@ ImageKnifeComponent({ | ToonTransformation | 动画滤波器(使用GPUIImage) | | VignetterTransformation | 装饰滤波器(使用GPUIImage) | +### 文件缓存FileCache接口 + +| 参数名称 | 入参内容 | 功能简介 | +|--------------|---------------------------------------------|------------------| +| reFileFolder | path: string | 切换文件缓存目录(不存在则创建) | +| reSize | size: number | 设置最大缓存数 | + ## 下载安装GPUImage依赖 方法一:在Terminal窗口中,执行如下命令安装三方包,DevEco Studio会自动在工程的oh-package.json5中自动添加三方包依赖。 ``` diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 816bdeb..9dc1f39 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -74,6 +74,11 @@ struct Index { }); }) + Button('测试切换文件缓存目录').margin({top:10}).onClick(()=>{ + router.push({ + uri: 'pages/TestReCacheFolderPage', + }); + }) Button("预加载图片到文件缓存").margin({top:10}).onClick(()=>{ router.push({ uri: 'pages/TestPrefetchToFileCache', diff --git a/entry/src/main/ets/pages/TestReCacheFolderPage.ets b/entry/src/main/ets/pages/TestReCacheFolderPage.ets new file mode 100644 index 0000000..322187e --- /dev/null +++ b/entry/src/main/ets/pages/TestReCacheFolderPage.ets @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { ImageKnifeComponent,ImageKnife,ImageKnifeOption } from '@ohos/libraryimageknife' + +@Entry +@Component +struct TestReCacheFolderPage { + @State imageKnifeOption: ImageKnifeOption = { + loadSrc:$r('app.media.startIcon'), + placeholderSrc:$r('app.media.loading'), + errorholderSrc:$r('app.media.failed') + } + @State message: string = "" + private bol: boolean = true + private url: string = 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp' + build() { + Column() { + Button("切换缓存目录").margin({top:10}).onClick(async ()=>{ + ImageKnife.getInstance() + .removeMemoryCache(this.url) + this.bol = !this.bol + ImageKnife.getInstance().getFileCache().reCacheFolder(this.bol ? "ImageKnife" : "ZFileCache") + }) + Button("预加载并返回文件缓存路径").margin({top:10}).onClick(()=>{ + ImageKnife.getInstance().preLoadCache(this.url) + .then((data)=>{ + this.imageKnifeOption.loadSrc = data + this.message = data + }) + }) + ImageKnifeComponent({ + imageKnifeOption: this.imageKnifeOption + }).width(300).height(300).margin({top:30}) + Text(this.message) + } + .height('100%') .width('100%') + } +} \ 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 84072cb..805dcb5 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -18,6 +18,7 @@ "pages/LoadStatePage", "pages/TestHspPreLoadImage", "pages/TestRemoveCache", - "pages/dataShareUriLoadPage" + "pages/dataShareUriLoadPage", + "pages/TestReCacheFolderPage" ] } \ No newline at end of file diff --git a/entry/src/ohosTest/ets/test/FileLruCache.test.ets b/entry/src/ohosTest/ets/test/FileLruCache.test.ets index 00ddc29..d1c6b3f 100644 --- a/entry/src/ohosTest/ets/test/FileLruCache.test.ets +++ b/entry/src/ohosTest/ets/test/FileLruCache.test.ets @@ -149,6 +149,31 @@ export default function FileLruCacheTest() { fileCache.put(JSON.stringify("xxxxx"),buf) expect(fileCache.get(JSON.stringify("xxxxx"))?.byteLength).assertEqual(1024 * 1024) }); + it('testReSize', 0, async () => { + const buf1: ArrayBuffer = new ArrayBuffer(1 * 1024 * 1024); + const buf2: ArrayBuffer = new ArrayBuffer(2 * 1024 * 1024); + const buf3: ArrayBuffer = new ArrayBuffer(3 * 1024 * 1024); + const buf4: ArrayBuffer = new ArrayBuffer(4 * 1024 * 1024); + + let fileCache: FileCache = new FileCache(GlobalContext.getContext() + .getObject("context") as Context, 5, 20 * 1024 * 1024) + await fileCache.initFileCache() + + console.info(Constants.TAG + JSON.stringify("xxxxx")) + fileCache.put("aaa",buf1) + fileCache.put("bbb",buf2) + fileCache.put("ccc",buf3) + fileCache.put("ddd",buf4) + expect(fileCache.get("aaa")?.byteLength).assertEqual(1 * 1024 * 1024) + expect(fileCache.get("bbb")?.byteLength).assertEqual(2 * 1024 * 1024) + expect(fileCache.get("ccc")?.byteLength).assertEqual(3 * 1024 * 1024) + expect(fileCache.get("ddd")?.byteLength).assertEqual(4 * 1024 * 1024) + fileCache.reSize(3) + expect(fileCache.get("aaa")?.byteLength).assertEqual(undefined) + expect(fileCache.get("bbb")?.byteLength).assertEqual(2 * 1024 * 1024) + expect(fileCache.get("ccc")?.byteLength).assertEqual(3 * 1024 * 1024) + expect(fileCache.get("ddd")?.byteLength).assertEqual(4 * 1024 * 1024) + }); }); } diff --git a/library/oh-package.json5 b/library/oh-package.json5 index a584ba0..c206a0f 100644 --- a/library/oh-package.json5 +++ b/library/oh-package.json5 @@ -14,7 +14,7 @@ "main": "index.ets", "repository": "https://gitee.com/openharmony-tpc/ImageKnife", "type": "module", - "version": "3.0.0-rc.7", + "version": "3.0.0-rc.8", "dependencies": { "@ohos/gpu_transform": "^1.0.2" }, diff --git a/library/src/main/ets/ImageKnife.ets b/library/src/main/ets/ImageKnife.ets index 19b4d08..0dfc2f1 100644 --- a/library/src/main/ets/ImageKnife.ets +++ b/library/src/main/ets/ImageKnife.ets @@ -166,10 +166,10 @@ export class ImageKnife { preLoadCache(loadSrc: string | ImageKnifeOption): Promise { return new Promise((resolve, reject) => { let imageKnifeOption = new ImageKnifeOption() - if (loadSrc instanceof ImageKnifeOption) { - imageKnifeOption = loadSrc + if (typeof loadSrc == "string") { + imageKnifeOption.loadSrc = loadSrc } else { - imageKnifeOption.loadSrc = loadSrc; + imageKnifeOption = loadSrc; } LogUtil.log("ImageKnife_DataTime_preLoadCache-imageKnifeOption:"+loadSrc) let fileKey = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature) diff --git a/library/src/main/ets/ImageKnifeDispatcher.ets b/library/src/main/ets/ImageKnifeDispatcher.ets index 56b81c3..e37add1 100644 --- a/library/src/main/ets/ImageKnifeDispatcher.ets +++ b/library/src/main/ets/ImageKnifeDispatcher.ets @@ -171,7 +171,8 @@ export class ImageKnifeDispatcher { signature: currentRequest.imageKnifeOption.signature, requestSource: requestSource, isWatchProgress: isWatchProgress, - memoryKey: memoryKey + memoryKey: memoryKey, + fileCacheFolder: ImageKnife.getInstance().getFileCache().getCacheFolder() } @@ -373,7 +374,7 @@ async function requestJob(request: RequestJobRequest, requestList?: List