Pre Merge pull request !283 from zgf/3.x

This commit is contained in:
zgf 2024-05-29 06:16:41 +00:00 committed by Gitee
commit fadfda89d3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 158 additions and 9 deletions

View File

@ -9,6 +9,9 @@
- 成功回调增加返回图片分辨率宽高
- 内存缓存时将pixelMap进行release释放
- 提供清理缓存能力
- 新增判断图片是否缓存接口
- 文件缓存初始化增加默认值
- 预加载接口新增返回加载错误信息
## 3.0.0-rc.4
- 支持hsp多包图片资源

View File

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

View File

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

View File

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

View File

@ -17,6 +17,7 @@
"pages/TestWriteCacheStage",
"pages/LoadStatePage",
"pages/TestHspPreLoadImage",
"pages/TestRemoveCache"
"pages/TestRemoveCache",
"pages/TestIsExistCachePage"
]
}

View File

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