补充写入缓存策略样例

Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
zgf 2024-05-10 10:38:24 +08:00
parent f350c32b2e
commit 8d00a54a4f
4 changed files with 83 additions and 1 deletions

View File

@ -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',

View File

@ -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%')
}
}

View File

@ -13,6 +13,7 @@
"pages/TestIsUrlExist",
"pages/TestHeader",
"pages/ImageTransformation",
"pages/ObjectFitPage"
"pages/ObjectFitPage",
"pages/TestWriteCacheStage"
]
}

View File

@ -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)