From cc68d2bc5d9d474a91c3c535ad146cb8bf6a0ddd Mon Sep 17 00:00:00 2001 From: zgf Date: Wed, 5 Jun 2024 15:04:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=8D=E7=94=A8=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E4=B8=8B=E4=BB=8E=E5=86=85=E5=AD=98=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=90=8E=E5=8F=88=E6=B8=85=E7=A9=BA=E4=BA=86?= =?UTF-8?q?=E7=94=BB=E5=B8=83=E5=AF=BC=E8=87=B4=E5=9B=BE=E7=89=87=E4=B8=8D?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zgf --- CHANGELOG.md | 1 + .../main/ets/components/imageknife/ImageKnife.ets | 2 +- .../components/imageknife/ImageKnifeComponent.ets | 13 ++++++++++--- .../ets/components/imageknife/RequestOption.ets | 4 ++-- .../imageknife/interface/AsyncCallback.ets | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0421e5..63b13dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - 支持多种组合变换 - 提供清理缓存能力 - 修复preLoad接口失效 +- 修复复用场景下从内存获取图片后又清空了画布导致图片不显示 ## 2.2.0-rc.2 - ImageKnife支持下采样 diff --git a/library/src/main/ets/components/imageknife/ImageKnife.ets b/library/src/main/ets/components/imageknife/ImageKnife.ets index f71d9e9..26175dc 100644 --- a/library/src/main/ets/components/imageknife/ImageKnife.ets +++ b/library/src/main/ets/components/imageknife/ImageKnife.ets @@ -744,7 +744,7 @@ export class ImageKnife { if (requestOption.requestState === ImageKnifeRequestState.PROGRESS) { requestOption.requestState = ImageKnifeRequestState.COMPLETE; } - requestOption.loadComplete(mainCache as ImageKnifeData); + requestOption.loadComplete(mainCache as ImageKnifeData,true); }) } return; diff --git a/library/src/main/ets/components/imageknife/ImageKnifeComponent.ets b/library/src/main/ets/components/imageknife/ImageKnifeComponent.ets index 209aed4..6334c94 100644 --- a/library/src/main/ets/components/imageknife/ImageKnifeComponent.ets +++ b/library/src/main/ets/components/imageknife/ImageKnifeComponent.ets @@ -36,6 +36,7 @@ interface KeyCanvas { @Component export struct ImageKnifeComponent { @Watch('watchImageKnifeOption') @ObjectLink imageKnifeOption: ImageKnifeOption; + isClearRect: boolean = true private settings: RenderingContextSettings = new RenderingContextSettings(true) private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) private hasDisplayRetryholder = false; @@ -171,6 +172,7 @@ export struct ImageKnifeComponent { } this.request = new RequestOption(); this.lastSrc = this.imageKnifeOption.loadSrc; + this.isClearRect = true this.whetherWaitSize(); } } @@ -215,7 +217,7 @@ export struct ImageKnifeComponent { configNecessary(request: RequestOption) { request.load(this.imageKnifeOption.loadSrc) - .addListener({ callback: (err:BusinessError|string, data:ImageKnifeData) => { + .addListener({ callback: (err:BusinessError|string, data:ImageKnifeData,isMainCache?:boolean) => { LogUtil.log('VISIBLE: ImageKnifeComponent request.load callback, err: ' + err); if (err != "" && this.imageKnifeOption.onLoadListener && this.imageKnifeOption.onLoadListener.onLoadFailed) { this.imageKnifeOption.onLoadListener.onLoadFailed(err as string); @@ -239,6 +241,10 @@ export struct ImageKnifeComponent { } else { this.isGif = false } + if(isMainCache) { + this.context.clearRect(0,0,this.context.width,this.context.height) + this.isClearRect = false + } if(this.lastSrc !== request.loadSrc && this.lastSrc !== ""){} else { // 组件 没有被销毁 执行渲染逻辑 @@ -780,9 +786,10 @@ export struct ImageKnifeComponent { } aboutToReuse(params: ESObject) { LogUtil.log('VISIBLE: Reuse happened!'); - this.context.clearRect(0,0,this.context.width,this.context.height) + if(this.isClearRect) { + this.context.clearRect(0,0,this.context.width,this.context.height) + } // watch方法会触发两次,第二次触发不会执行加载逻辑,reuse比watch晚,因此需要手动在此处触发加载逻辑 - this.whetherWaitSize(); } aboutToAppear() { LogUtil.log('VISIBLE: ImageKnifeComponent aboutToAppear happened!') diff --git a/library/src/main/ets/components/imageknife/RequestOption.ets b/library/src/main/ets/components/imageknife/RequestOption.ets index edbf5a7..ac7695e 100644 --- a/library/src/main/ets/components/imageknife/RequestOption.ets +++ b/library/src/main/ets/components/imageknife/RequestOption.ets @@ -591,13 +591,13 @@ export class RequestOption { fallbackOnError = (error: BusinessError | string) => { LogUtil.error("失败占位图解析失败 error =" + JSON.stringify(error)); } - loadComplete = (imageKnifeData: ImageKnifeData) => { + loadComplete = (imageKnifeData: ImageKnifeData,isMainCache?:boolean) => { this.loadMainReady = true; // 三级缓存数据加载成功 if (this.requestListeners != undefined) { for (let i = 0; i < this.requestListeners.length; i++) { let requestListener = this.requestListeners[i]; - let boolInterception = requestListener.callback("", imageKnifeData); + let boolInterception = requestListener.callback("", imageKnifeData,isMainCache); if (boolInterception) { break; } diff --git a/library/src/main/ets/components/imageknife/interface/AsyncCallback.ets b/library/src/main/ets/components/imageknife/interface/AsyncCallback.ets index be30df4..72e4794 100644 --- a/library/src/main/ets/components/imageknife/interface/AsyncCallback.ets +++ b/library/src/main/ets/components/imageknife/interface/AsyncCallback.ets @@ -14,5 +14,5 @@ */ export interface AsyncCallback { - callback:(err: string, data: T)=>boolean; + callback:(err: string, data: T,isMainCache?:boolean)=>boolean; }