diff --git a/CHANGELOG.md b/CHANGELOG.md index 70da81f..b37b517 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.0.1-rc.2 +- 修复自定义下载失败无失败回调 +- 增加全局配置自定义下载接口 + ## 3.0.1-rc.1 - 新增ImageKnifeAnimatorComponent控制动图组件 - 修复部分heif图无法解码 diff --git a/README.md b/README.md index fe58b14..3f50768 100644 --- a/README.md +++ b/README.md @@ -314,6 +314,7 @@ ImageKnifeAnimatorComponent({ | addHeader | key: string, value: Object | 全局添加http请求头 | | setHeaderOptions | Array | 全局设置http请求头 | | deleteHeader | key: string | 全局删除http请求头 | +| setCustomGetImage | customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise | 全局设置自定义下载 | | setEngineKeyImpl | IEngineKey | 全局配置缓存key生成策略 | | putCacheImage | url: string, pixelMap: PixelMap, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string | 写入内存磁盘缓存 | | removeMemoryCache| url: string | ImageKnifeOption | 清理指定内存缓存 | diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index ee212d9..06da551 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -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', diff --git a/entry/src/main/ets/pages/LoadStatePage.ets b/entry/src/main/ets/pages/LoadStatePage.ets index e863e7c..af8689a 100644 --- a/entry/src/main/ets/pages/LoadStatePage.ets +++ b/entry/src/main/ets/pages/LoadStatePage.ets @@ -36,6 +36,10 @@ struct LoadStatePage { }, border: { radius: 50 } } + @State imageKnifeOption1: ImageKnifeOption = { + loadSrc: $r('app.media.startIcon') + } + @State message: string = "" @State currentWidth: number = 200 @State currentHeight: number = 200 @State typeValue: string = "" @@ -78,11 +82,33 @@ struct LoadStatePage { Text(this.typeValue) ImageKnifeComponent({ imageKnifeOption: this.ImageKnifeOption }).height(this.currentHeight).width(this.currentWidth) .margin({ top: 20 }) - + Button("自定义下载失败").onClick(()=>{ + this.imageKnifeOption1 = { + loadSrc: "abc", + placeholderSrc:$r('app.media.loading'), + errorholderSrc:$r('app.media.failed'), + customImage:custom, + onLoadListener: { + onLoadFailed:(err)=>{ + this.message = "err:" + err + } + } + } + }).margin({ top: 20 }) + Text(this.message).fontSize(20).margin({ top: 20 }) + ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }).height(this.currentHeight).width(this.currentWidth) + .margin({ top: 20 }) } .width('100%') .height('100%') } +} +// 自定义下载方法 +@Concurrent +async function custom(context: Context, src: string | PixelMap | Resource): Promise { + console.info("ImageKnife:: custom download:" + src) + // 举例写死从本地文件读取,也可以自己请求网络图片 + return undefined } \ No newline at end of file diff --git a/entry/src/main/ets/pages/TestSetCustomImagePage.ets b/entry/src/main/ets/pages/TestSetCustomImagePage.ets new file mode 100644 index 0000000..809769f --- /dev/null +++ b/entry/src/main/ets/pages/TestSetCustomImagePage.ets @@ -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 { + console.info("ImageKnife:: custom download:" + src) + // 举例写死从本地文件读取,也可以自己请求网络图片 + return context.resourceManager.getMediaContentSync($r("app.media.pngSample").id).buffer as ArrayBuffer +} \ 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 83d9632..2b6e3e7 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -20,6 +20,7 @@ "pages/TestRemoveCache", "pages/dataShareUriLoadPage", "pages/TestCommonImage", - "pages/ImageAnimatorPage" + "pages/ImageAnimatorPage", + "pages/TestSetCustomImagePage" ] } \ No newline at end of file diff --git a/library/oh-package.json5 b/library/oh-package.json5 index 1d80900..67af1d6 100644 --- a/library/oh-package.json5 +++ b/library/oh-package.json5 @@ -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" }, diff --git a/library/src/main/ets/ImageKnife.ets b/library/src/main/ets/ImageKnife.ets index 527156d..d168038 100644 --- a/library/src/main/ets/ImageKnife.ets +++ b/library/src/main/ets/ImageKnife.ets @@ -38,7 +38,7 @@ export class ImageKnife { private _isRequestInSubThread: boolean = true; //定义全局网络请求header map headerMap: Map = new Map(); - + customGetImage: ((context: Context, src: string | PixelMap | Resource) => Promise) | undefined = undefined public static getInstance(): ImageKnife { if (!ImageKnife.instance) { ImageKnife.instance = new ImageKnife(); @@ -380,4 +380,14 @@ export class ImageKnife { getEngineKeyImpl(): IEngineKey { return this.dispatcher.getEngineKeyImpl(); } + /** + * 全局设置自定义下载 + * @param customGetImage 自定义请求函数 + */ + setCustomGetImage(customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise) { + this.customGetImage = customGetImage + } + getCustomGetImage(): undefined | ((context: Context, src: string | PixelMap | Resource) => Promise){ + return this.customGetImage + } } \ No newline at end of file diff --git a/library/src/main/ets/ImageKnifeDispatcher.ets b/library/src/main/ets/ImageKnifeDispatcher.ets index a6d896a..86ac447 100644 --- a/library/src/main/ets/ImageKnifeDispatcher.ets +++ b/library/src/main/ets/ImageKnifeDispatcher.ets @@ -179,7 +179,9 @@ export class ImageKnifeDispatcher { isAnimator:isAnimator } - + if(request.customGetImage == undefined) { + request.customGetImage = ImageKnife.getInstance().getCustomGetImage() + } if (ImageKnife.getInstance().isRequestInSubThread){ // 启动线程下载和解码主图 LogUtil.log("ImageKnife_DataTime_getAndShowImage_Task.start:" + currentRequest.imageKnifeOption.loadSrc) @@ -399,6 +401,7 @@ async function requestJob(request: RequestJobRequest, requestList?: List