/*
* Copyright (C) 2025 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 { ImageKnife, ImageKnifeOptionV2, ImageKnifeComponentV2 } from '@ohos/libraryimageknife'
import { PageViewModel } from './model/PageViewModel'
@Entry
@ComponentV2
struct PreloadAndRemoveV2 {
@Local imageKnifeOption: ImageKnifeOptionV2 = new ImageKnifeOptionV2({
loadSrc:$r('app.media.startIcon'),
placeholderSrc:$r('app.media.loading'),
errorholderSrc:$r('app.media.failed')
})
preloadUrlWithFile() {
ImageKnife.getInstance().preLoadCache(new ImageKnifeOptionV2({
loadSrc:PageViewModel.getGifMenus()[0],
})).then((cachePath)=>{
this.imageKnifeOption.loadSrc = cachePath
}).catch((err: string)=>{
this.imageKnifeOption.loadSrc = err
})
}
preloadUrl() {
ImageKnife.getInstance().preload(new ImageKnifeOptionV2({
loadSrc: PageViewModel.getMenus()[2],
onLoadListener: {
onLoadSuccess:()=>{
this.imageKnifeOption.loadSrc = PageViewModel.getMenus()[2]
}
}
}))
}
build() {
Column() {
Button($r('app.string.Preloading_images_to_file_cache_using_option'))
.onClick(()=>{
this.preloadUrlWithFile()
})
Button($r('app.string.Load_image_offline_after_preloading'))
.onClick(()=>{
this.preloadUrl()
}).margin({top: 10})
Button($r('app.string.Test_removing_image_cache_interface'))
.onClick(()=>{
ImageKnife.getInstance().removeFileCache(new ImageKnifeOptionV2({
loadSrc: PageViewModel.getGifMenus()[0]
}))
ImageKnife.getInstance().removeMemoryCache(new ImageKnifeOptionV2({
loadSrc: PageViewModel.getGifMenus()[0]
}))
}).margin({top: 10})
ImageKnifeComponentV2({
imageKnifeOption: this.imageKnifeOption
})
}.width('100%')
.height('100%')
}
}