diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 34dfc67..34c003f 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -86,7 +86,12 @@ struct Index { }); }) + Button("测试写入缓存策略").margin({top:10}).onClick(()=>{ + router.push({ + uri: 'pages/TestWriteCacheStage', + }); + }) Button("图片变换").margin({top:10}).onClick(()=>{ router.push({ uri: 'pages/ImageTransformation', diff --git a/entry/src/main/ets/pages/TestWriteCacheStage.ets b/entry/src/main/ets/pages/TestWriteCacheStage.ets new file mode 100644 index 0000000..43cd10a --- /dev/null +++ b/entry/src/main/ets/pages/TestWriteCacheStage.ets @@ -0,0 +1,74 @@ +/* + * 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,CacheStrategy,ImageKnifeOption } from '@ohos/imageknife' + +@Entry +@Component +struct TestWriteCacheStage { + @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 imageKnifeOption3: ImageKnifeOption = { + loadSrc:$r('app.media.startIcon'), + placeholderSrc:$r('app.media.loading'), + errorholderSrc:$r('app.media.failed') + } + + build() { + Column() { + Button("写入内存文件缓存").margin({top:10}).onClick(async ()=>{ + this.imageKnifeOption1 = { + loadSrc:'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp', + placeholderSrc:$r('app.media.loading'), + errorholderSrc:$r('app.media.failed'), + writeCacheStrategy:CacheStrategy.Default + } + }) + ImageKnifeComponent({ + imageKnifeOption: this.imageKnifeOption1 + }).width(200).height(200).margin({top:10}) + Button("写入内存缓存").margin({top:10}).onClick(async ()=>{ + this.imageKnifeOption2 = { + loadSrc:"https://hbimg.huabanimg.com/cc6af25f8d782d3cf3122bef4e61571378271145735e9-vEVggB", + placeholderSrc:$r('app.media.loading'), + errorholderSrc:$r('app.media.failed'), + writeCacheStrategy:CacheStrategy.Memory + } + }) + ImageKnifeComponent({ + imageKnifeOption: this.imageKnifeOption2 + }).width(200).height(200).margin({top:10}) + Button("写入文件缓存").margin({top:10}).onClick(async ()=>{ + this.imageKnifeOption3 = { + loadSrc:'https://img-blog.csdn.net/20140514114029140', + placeholderSrc:$r('app.media.loading'), + errorholderSrc:$r('app.media.failed'), + writeCacheStrategy:CacheStrategy.File + } + }) + ImageKnifeComponent({ + imageKnifeOption: this.imageKnifeOption3 + }).width(200).height(200).margin({top:10}) + } + .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 c8c3c14..4448677 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -13,6 +13,7 @@ "pages/TestIsUrlExist", "pages/TestHeader", "pages/ImageTransformation", - "pages/ObjectFitPage" + "pages/ObjectFitPage", + "pages/TestWriteCacheStage" ] } \ No newline at end of file diff --git a/library/src/main/ets/ImageKnifeDispatcher.ets b/library/src/main/ets/ImageKnifeDispatcher.ets index f4459fd..3ba5f03 100644 --- a/library/src/main/ets/ImageKnifeDispatcher.ets +++ b/library/src/main/ets/ImageKnifeDispatcher.ets @@ -168,6 +168,7 @@ export class ImageKnifeDispatcher { } // 保存文件缓存 if (requestJobResult.bufferSize > 0 && currentRequest.imageKnifeOption.writeCacheStrategy !== CacheStrategy.Memory) { + LogUtil.log("ImageKnifeComponent writeCacheStrategy File") ImageKnife.getInstance().saveWithoutWriteFile(requestJobResult.fileKey, requestJobResult.bufferSize) } @@ -179,6 +180,7 @@ export class ImageKnifeDispatcher { // 保存内存缓存 if (currentRequest.imageKnifeOption.writeCacheStrategy !== CacheStrategy.File) { + LogUtil.log("ImageKnifeComponent writeCacheStrategy Memory") ImageKnife.getInstance() .saveMemoryCache(this.engineKey.generateMemoryKey(imageSrc, requestSource, currentRequest.imageKnifeOption), ImageKnifeData)