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; }