修复自定义下载失败无失败回调以及新增全局自定义下载接口

Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
zgf 2024-07-26 11:35:23 +08:00
parent 19e1ba9528
commit 8793e8c91f
8 changed files with 93 additions and 3 deletions

View File

@ -1,3 +1,7 @@
## 3.0.1-rc.2
- 修复自定义下载失败无失败回调
- 增加全局配置自定义下载接口
## 3.0.1-rc.1
- 新增ImageKnifeAnimatorComponent控制动图组件
- 修复部分heif图无法解码

View File

@ -314,6 +314,7 @@ ImageKnifeAnimatorComponent({
| addHeader | key: string, value: Object | 全局添加http请求头 |
| setHeaderOptions | Array<HeaderOptions> | 全局设置http请求头 |
| deleteHeader | key: string | 全局删除http请求头 |
| setCustomGetImage| customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined> | 全局设置自定义下载 |
| setEngineKeyImpl | IEngineKey | 全局配置缓存key生成策略 |
| putCacheImage | url: string, pixelMap: PixelMap, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string | 写入内存磁盘缓存 |
| removeMemoryCache| url: string | ImageKnifeOption | 清理指定内存缓存 |

View File

@ -48,6 +48,12 @@ struct Index {
});
})
Button("全局自定义下载").margin({top:10}).onClick(()=>{
router.push({
uri: 'pages/TestSetCustomImagePage',
});
})
Button("多图 + LazyForEach").margin({top:10}).onClick(()=>{
router.push({
uri: 'pages/ManyPhotoShowPage',

View File

@ -0,0 +1,64 @@
/*
* 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 } from '@ohos/libraryimageknife'
@Entry
@Component
struct TestSetCustomImagePage {
@State imageKnifeOption: ImageKnifeOption = {
loadSrc: $r('app.media.startIcon'),
placeholderSrc: $r('app.media.loading')
}
aboutToAppear(): void {
ImageKnife.getInstance().setCustomGetImage(custom)
}
aboutToDisappear(): void {
ImageKnife.getInstance().setCustomGetImage()
}
build() {
Column() {
Button("自定义下载a").onClick(()=>{
this.imageKnifeOption = {
loadSrc: "aaa",
placeholderSrc: $r('app.media.loading')
}
})
Button("自定义下载b").onClick(()=>{
this.imageKnifeOption = {
loadSrc: "bbb",
placeholderSrc: $r('app.media.loading')
}
})
Button("自定义下载c").onClick(()=>{
this.imageKnifeOption = {
loadSrc: "ccc",
placeholderSrc: $r('app.media.loading')
}
})
ImageKnifeComponent({
imageKnifeOption: this.imageKnifeOption
}).width(300)
.height(300)
}
.width("100%")
.height("100%")
}
}
@Concurrent
async function custom(context: Context, src: string | PixelMap | Resource): Promise<ArrayBuffer | undefined> {
console.info("ImageKnife:: custom download" + src)
// 举例写死从本地文件读取,也可以自己请求网络图片
return context.resourceManager.getMediaContentSync($r("app.media.pngSample").id).buffer as ArrayBuffer
}

View File

@ -20,6 +20,7 @@
"pages/TestRemoveCache",
"pages/dataShareUriLoadPage",
"pages/TestCommonImage",
"pages/ImageAnimatorPage"
"pages/ImageAnimatorPage",
"pages/TestSetCustomImagePage"
]
}

View File

@ -14,7 +14,7 @@
"main": "index.ets",
"repository": "https://gitee.com/openharmony-tpc/ImageKnife",
"type": "module",
"version": "3.0.1-rc.1",
"version": "3.0.1-rc.2",
"dependencies": {
"@ohos/gpu_transform": "^1.0.2"
},

View File

@ -38,6 +38,7 @@ export class ImageKnife {
private _isRequestInSubThread: boolean = true;
//定义全局网络请求header map
headerMap: Map<string, Object> = new Map<string, Object>();
customGetImage: ((context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined>) | undefined = undefined
public static getInstance(): ImageKnife {
if (!ImageKnife.instance) {
@ -380,4 +381,14 @@ export class ImageKnife {
getEngineKeyImpl(): IEngineKey {
return this.dispatcher.getEngineKeyImpl();
}
/**
* 全局设置自定义下载
* @param customGetImage 自定义请求函数
*/
setCustomGetImage(customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined>) {
this.customGetImage = customGetImage
}
getCustomGetImage(): undefined | ((context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined>){
return this.customGetImage
}
}

View File

@ -178,7 +178,9 @@ export class ImageKnifeDispatcher {
fileCacheFolder: ImageKnife.getInstance().getFileCache().getCacheFolder(),
isAnimator:isAnimator
}
if(request.customGetImage == undefined) {
request.customGetImage = ImageKnife.getInstance().getCustomGetImage()
}
if (ImageKnife.getInstance().isRequestInSubThread){
// 启动线程下载和解码主图
@ -399,6 +401,7 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
if (resBuf === undefined) {
LogUtil.log("customGetImage customGetImage");
resBuf = await request.customGetImage(request.context, request.src)
loadError = resBuf == undefined ? "customGetImage loadFile" : loadError
// 保存文件缓存
if (resBuf !== undefined && request.writeCacheStrategy !== CacheStrategy.Memory) {
let copyBuf = buffer.concat([buffer.from(resBuf)]).buffer; // IDE有bug不能直接获取resBuf.byteLength