From c50acfe89613141f33b9d759bd787d3be437486c Mon Sep 17 00:00:00 2001 From: zgf Date: Sat, 25 May 2024 16:17:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=88=A4=E6=96=AD=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E6=98=AF=E5=90=A6=E7=BC=93=E5=AD=98=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E3=80=81=E6=96=87=E4=BB=B6=E7=BC=93=E5=AD=98=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E5=A2=9E=E5=8A=A0=E9=BB=98=E8=AE=A4=E5=80=BC=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E9=A2=84=E5=8A=A0=E8=BD=BD=E6=8E=A5=E5=8F=A3=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=BF=94=E5=9B=9E=E5=8A=A0=E8=BD=BD=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zgf --- CHANGELOG.md | 3 + entry/src/main/ets/pages/Index.ets | 10 +- .../main/ets/pages/TestIsExistCachePage.ets | 97 +++++++++++++++++++ .../ets/pages/TestPrefetchToFileCache.ets | 11 ++- .../resources/base/profile/main_pages.json | 3 +- library/src/main/ets/ImageKnife.ets | 43 +++++++- 6 files changed, 158 insertions(+), 9 deletions(-) create mode 100644 entry/src/main/ets/pages/TestIsExistCachePage.ets diff --git a/CHANGELOG.md b/CHANGELOG.md index b9fa439..9605241 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ - 成功回调增加返回图片分辨率宽高 - 内存缓存时将pixelMap进行release释放 - 提供清理缓存能力 +- 新增判断图片是否缓存接口 +- 文件缓存初始化增加默认值 +- 预加载接口新增返回加载错误信息 ## 3.0.0-rc.4 - 支持hsp多包图片资源 diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index a9666dc..7a2ff8f 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -26,13 +26,19 @@ struct Index { build() { Column() { - Button("测试HSP场景预加载").onClick(()=>{ + Button("测试图片是否缓存接口").margin({top:10}).onClick(()=>{ + router.push({ + uri: 'pages/TestIsExistCachePage', + + }); + }) + Button("测试HSP场景预加载").margin({top:10}).onClick(()=>{ router.push({ uri: 'pages/TestHspPreLoadImage', }); }) - Button("单个图片使用").onClick(()=>{ + Button("单个图片使用").margin({top:10}).onClick(()=>{ router.push({ uri: 'pages/SingleImage', diff --git a/entry/src/main/ets/pages/TestIsExistCachePage.ets b/entry/src/main/ets/pages/TestIsExistCachePage.ets new file mode 100644 index 0000000..26ed5a9 --- /dev/null +++ b/entry/src/main/ets/pages/TestIsExistCachePage.ets @@ -0,0 +1,97 @@ +/* + * 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,CacheStrategy } from '@ohos/libraryimageknife' + +@Entry +@Component +struct TestPrefetchToFileCachePage { + @State imageKnifeOption: ImageKnifeOption = { + loadSrc:$r('app.media.startIcon'), + placeholderSrc:$r('app.media.loading'), + errorholderSrc:$r('app.media.failed') + } + @State imageKnifeOption1: ImageKnifeOption = { + loadSrc:$r('app.media.startIcon'), + placeholderSrc:$r('app.media.loading'), + errorholderSrc:$r('app.media.failed') + } + @State imageKnifeOption2: ImageKnifeOption = { + loadSrc:$r('app.media.startIcon'), + placeholderSrc:$r('app.media.loading'), + errorholderSrc:$r('app.media.failed') + } + @State url: string = 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp' + @State message: string = "" + preload(url:string) { + ImageKnife.getInstance().preLoadCache(url).then((fileCachePath: string)=>{ + console.log("preload-fileCachePath=="+ fileCachePath) + }).catch((err:string)=>{ + console.log("preload-fileCachePath==error=="+ err) + }) + } + build() { + Column() { + Flex(){ + Button("写入内存缓存").onClick(()=>{ + ImageKnife.getInstance() + .removeMemoryCache(this.url) + ImageKnife.getInstance() + .removeFileCache(this.url) + this.imageKnifeOption.writeCacheStrategy = CacheStrategy.Memory + this.imageKnifeOption.loadSrc = this.url + }) + Button("写入文件缓存").onClick(()=>{ + ImageKnife.getInstance() + .removeMemoryCache(this.url) + ImageKnife.getInstance() + .removeFileCache(this.url) + this.imageKnifeOption.writeCacheStrategy = CacheStrategy.File + this.imageKnifeOption.loadSrc = this.url + }) + Button("内存缓存是否缓存").onClick(()=>{ + let isExist = ImageKnife.getInstance().IsExistCacheImage(this.url,CacheStrategy.Memory) + if (isExist) { + this.imageKnifeOption1.loadSrc = this.url + this.message = "" + } else { + this.imageKnifeOption1.loadSrc = $r('app.media.startIcon') + this.message = "该图片未缓存到内存缓存中" + } + }) + Button("文件缓存是否缓存").onClick(()=>{ + let isExist = ImageKnife.getInstance().IsExistCacheImage(this.url,CacheStrategy.File) + if (isExist) { + this.imageKnifeOption2.loadSrc = this.url + this.message = "" + } else { + this.imageKnifeOption2.loadSrc = $r('app.media.startIcon') + this.message = "该图片未缓存到文件缓存中" + } + }) + } + ImageKnifeComponent({ + imageKnifeOption: this.imageKnifeOption + }).width(200).height(200).margin({top:10}) + ImageKnifeComponent({ + imageKnifeOption: this.imageKnifeOption1 + }).width(200).height(200).margin({top:10}) + ImageKnifeComponent({ + imageKnifeOption: this.imageKnifeOption2 + }).width(200).height(200).margin({top:10}) + Text(this.message).fontColor(Color.Red).fontSize(20) + } + .height('100%') .width('100%') + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/TestPrefetchToFileCache.ets b/entry/src/main/ets/pages/TestPrefetchToFileCache.ets index d6b9e23..6ae85e4 100644 --- a/entry/src/main/ets/pages/TestPrefetchToFileCache.ets +++ b/entry/src/main/ets/pages/TestPrefetchToFileCache.ets @@ -22,14 +22,17 @@ struct TestPrefetchToFileCachePage { placeholderSrc:$r('app.media.loading'), errorholderSrc:$r('app.media.failed') } - async preload(url:string) { - let fileCachePath = await ImageKnife.getInstance().preLoadCache(url) - console.log("preload-fileCachePath=="+ fileCachePath) + preload(url:string) { + ImageKnife.getInstance().preLoadCache(url).then((fileCachePath: string)=>{ + console.log("preload-fileCachePath=="+ fileCachePath) + }).catch((err:string)=>{ + console.log("preload-fileCachePath==error=="+ err) + }) } build() { Column() { Button("预加载图片到文件缓存").margin({top:10}).onClick(async ()=>{ - await this.preload("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658") + this.preload("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658") }) Button("加载图片(预加载后可断网加载)").margin({top:10}).onClick(()=>{ this.imageKnifeOption.loadSrc = "https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658" diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index cd3a400..38f4798 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -17,6 +17,7 @@ "pages/TestWriteCacheStage", "pages/LoadStatePage", "pages/TestHspPreLoadImage", - "pages/TestRemoveCache" + "pages/TestRemoveCache", + "pages/TestIsExistCachePage" ] } \ No newline at end of file diff --git a/library/src/main/ets/ImageKnife.ets b/library/src/main/ets/ImageKnife.ets index 5dbe14b..cb6cfe6 100644 --- a/library/src/main/ets/ImageKnife.ets +++ b/library/src/main/ets/ImageKnife.ets @@ -53,7 +53,7 @@ export class ImageKnife { * @param size 缓存数量 * @param memory 内存大小 */ - async initFileCache(context: Context, size: number, memory: number) { + async initFileCache(context: Context, size: number = 256, memory: number = 256 * 1024 * 1024) { this.fileCache = new FileCache(context, size, memory) await this.fileCache.initFileCache() } @@ -163,6 +163,14 @@ export class ImageKnife { let fileKey = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature) let cachePath = ImageKnife.getInstance().getFileCache().getFileToPath(fileKey) if (cachePath == null || cachePath == "" || cachePath == undefined) { + imageKnifeOption.onLoadListener = { + onLoadSuccess(){ + resolve(ImageKnife.getInstance().getFileCache().getFileToPath(fileKey)) + }, + onLoadFailed(err) { + reject(err) + } + } let request = new ImageKnifeRequest( imageKnifeOption, imageKnifeOption.context !== undefined ? imageKnifeOption.context : getContext(this) as common.UIAbilityContext, @@ -171,7 +179,6 @@ export class ImageKnife { 0, { showPixelMap(version: number, pixelMap: PixelMap | string) { - resolve(ImageKnife.getInstance().getFileCache().getFileToPath(fileKey)) } } ) @@ -235,6 +242,38 @@ export class ImageKnife { } } + /** + * 判断内存或文件缓存是否已缓存 + * @param loadSrc 图片url + * @param cacheType 缓存策略 + * @param signature key自定义信息 + * @returns boolean + */ + IsExistCacheImage(loadSrc: string,cacheType: CacheStrategy = CacheStrategy.Default , signature?: string): boolean{ + let option: ImageKnifeOption = { + loadSrc: loadSrc, + signature: signature + } + let engineKeyImpl: IEngineKey = this.getEngineKeyImpl() + let isExist: boolean + if (cacheType == CacheStrategy.Memory) { + isExist = this.readMemoryCache(loadSrc,option,engineKeyImpl) == undefined ? false : true + } else if (cacheType == CacheStrategy.File) { + let fileKey = this.getEngineKeyImpl().generateFileKey(loadSrc, signature); + let cachePath = ImageKnife.getInstance().getFileCache().getFileToPath(fileKey) + isExist = cachePath == "" ? false : true + } else { + isExist = this.readMemoryCache(loadSrc,option,engineKeyImpl) == undefined ? false : true + if(isExist) {} + else { + let fileKey = this.getEngineKeyImpl().generateFileKey(loadSrc, signature); + let cachePath = ImageKnife.getInstance().getFileCache().getFileToPath(fileKey) + isExist = cachePath == "" ? false : true + } + } + return isExist + } + private pixelMapToArrayBuffer(pixelMap: PixelMap): ArrayBuffer { let imageInfo = pixelMap.getImageInfoSync(); let readBuffer: ArrayBuffer = new ArrayBuffer(imageInfo.size.height * imageInfo.size.width * 4);