From 46b12bf7c2c7aa23b4dff9fc3e7ceb423205a873 Mon Sep 17 00:00:00 2001 From: madixin Date: Mon, 8 Apr 2024 08:41:15 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E5=A4=8D=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E9=94=99=E4=BD=8D=E6=98=BE=E7=A4=BA=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9A=E9=80=9A=E8=BF=87watchoption=E5=8F=98?= =?UTF-8?q?=E5=8C=96=E5=90=8E=E5=8F=91=E8=B5=B7=E8=AF=B7=E6=B1=82=202.?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B1=95=E4=BD=8D=E5=9B=BE=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=9B=BE=EF=BC=8C=E6=B2=A1=E6=9C=89=E4=BB=8E=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=8B=BF=E5=8F=96=E7=9A=84=E9=97=AE=E9=A2=98=203.=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E8=AF=B7=E6=B1=82=E8=B6=85=E8=BF=87=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E6=94=BE=E9=98=9F=E5=88=97=E5=90=8E=E4=BB=8D?= =?UTF-8?q?=E7=84=B6=E7=9B=B4=E6=8E=A5=E4=B8=8B=E5=8F=91=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: madixin --- .gitignore | 7 +++--- entry/oh-package-lock.json5 | 13 ---------- .../main/ets/pages/{index.ets => Index.ets} | 0 library/src/main/ets/ImageKnifeDispatcher.ets | 15 +++++++++--- library/src/main/ets/ImageKnifeRequest.ets | 16 +++++++++---- .../ets/components/ImageKnifeComponent.ets | 24 +++++++------------ 6 files changed, 35 insertions(+), 40 deletions(-) delete mode 100644 entry/oh-package-lock.json5 rename entry/src/main/ets/pages/{index.ets => Index.ets} (100%) diff --git a/.gitignore b/.gitignore index 0b73d89..fbabf77 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,11 @@ /node_modules +/oh_modules /local.properties /.idea **/build -/oh_modules/ -/.hvigor/ -/oh-package-lock.json5 +/.hvigor +.cxx /.clangd /.clang-format /.clang-tidy +**/.test \ No newline at end of file diff --git a/entry/oh-package-lock.json5 b/entry/oh-package-lock.json5 deleted file mode 100644 index 2555096..0000000 --- a/entry/oh-package-lock.json5 +++ /dev/null @@ -1,13 +0,0 @@ -{ - "lockfileVersion": 3, - "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", - "specifiers": { - "@ohos/imageknife@../library": "@ohos/imageknife@../library" - }, - "packages": { - "@ohos/imageknife@../library": { - "mtime": "1711870193874.5308", - "resolved": "../library" - } - } -} \ No newline at end of file diff --git a/entry/src/main/ets/pages/index.ets b/entry/src/main/ets/pages/Index.ets similarity index 100% rename from entry/src/main/ets/pages/index.ets rename to entry/src/main/ets/pages/Index.ets diff --git a/library/src/main/ets/ImageKnifeDispatcher.ets b/library/src/main/ets/ImageKnifeDispatcher.ets index f24864f..b12dd12 100644 --- a/library/src/main/ets/ImageKnifeDispatcher.ets +++ b/library/src/main/ets/ImageKnifeDispatcher.ets @@ -43,13 +43,14 @@ export class ImageKnifeDispatcher { enqueue(request: ImageKnifeRequest): void { //1.内存有的话直接渲染 - if (request.showFromMemomry()) { + if (request.showFromMemomry(request.ImageKnifeOption.loadSrc, ImageKnifeRequestSource.SRC)) { return } //2.判断是否要排队 if (this.executingJobMap.length > this.maxRequests) { this.jobQueue.add(request) + return } this.executeJob(request) } @@ -57,7 +58,9 @@ export class ImageKnifeDispatcher { executeJob(request: ImageKnifeRequest): void { // 加载占位符 if (request.ImageKnifeOption.placeholderSrc !== undefined) { - this.getAndShowImage(request, request.ImageKnifeOption.placeholderSrc, ImageKnifeRequestSource.PLACE_HOLDER) + if (request.showFromMemomry(request.ImageKnifeOption.placeholderSrc, ImageKnifeRequestSource.PLACE_HOLDER) === false) { + this.getAndShowImage(request, request.ImageKnifeOption.placeholderSrc, ImageKnifeRequestSource.PLACE_HOLDER) + } } // 加载主图 @@ -102,7 +105,10 @@ export class ImageKnifeDispatcher { if (requestList !== undefined) { requestList.forEach((requestWithSource: ImageKnifeRequestWithSource) => { if (requestWithSource.source === ImageKnifeRequestSource.SRC && currentRequest.ImageKnifeOption.errorholderSrc !== undefined) { - this.getAndShowImage(currentRequest, currentRequest.ImageKnifeOption.errorholderSrc, ImageKnifeRequestSource.ERROR_HOLDER) + + if (currentRequest.showFromMemomry(currentRequest.ImageKnifeOption.errorholderSrc, ImageKnifeRequestSource.ERROR_HOLDER) === false) { + this.getAndShowImage(currentRequest, currentRequest.ImageKnifeOption.errorholderSrc, ImageKnifeRequestSource.ERROR_HOLDER) + } } }); this.executingJobMap.remove(key) @@ -130,6 +136,9 @@ export class ImageKnifeDispatcher { // todo 判断request生命周期,已销毁的不需要再绘制 // key相同的request,一起绘制 requestList.forEach((requestWithSource: ImageKnifeRequestWithSource) => { + if (requestWithSource.request.requestState === ImageKnifeRequestState.DESTROY){ + return + } // 画主图 if (requestWithSource.source === ImageKnifeRequestSource.SRC || requestWithSource.source === ImageKnifeRequestSource.ERROR_HOLDER || (requestWithSource.source === ImageKnifeRequestSource.PLACE_HOLDER && requestWithSource.request.requestState === ImageKnifeRequestState.PROGRESS)) { diff --git a/library/src/main/ets/ImageKnifeRequest.ets b/library/src/main/ets/ImageKnifeRequest.ets index 53a0f1e..6a0e38f 100644 --- a/library/src/main/ets/ImageKnifeRequest.ets +++ b/library/src/main/ets/ImageKnifeRequest.ets @@ -18,6 +18,7 @@ import { ImageKnife } from './ImageKnife' import common from '@ohos.app.ability.common'; import { SparkMD5 } from './3rd_party/sparkmd5/spark-md5' import { LogUtil } from './utils/LogUtil' +import { ImageKnifeRequestSource } from './ImageKnifeDispatcher'; export class ImageKnifeRequest { @@ -33,7 +34,7 @@ export class ImageKnifeRequest { uIAbilityContext: common.UIAbilityContext, width: number, height: number, - version:number, + version: number, ImageKnifeRequestCallback: ImageKnifeRequestCallback) { this.ImageKnifeOption = option this.context = uIAbilityContext @@ -43,14 +44,19 @@ export class ImageKnifeRequest { this.ImageKnifeRequestCallback = ImageKnifeRequestCallback } - showFromMemomry(): boolean { + showFromMemomry(imageSrc: string | PixelMap | Resource, requestSource: ImageKnifeRequestSource): boolean { let memoryCache: ImageKnifeData | undefined = ImageKnife.getInstance() - .loadFromMemoryCache(this.generateKey(this.ImageKnifeOption.loadSrc)) + .loadFromMemoryCache(this.generateKey(imageSrc)) if (memoryCache !== undefined) { - LogUtil.log("load from memory cache for key = " + this.generateKey(this.ImageKnifeOption.loadSrc)) // 画主图 if (this.requestState === ImageKnifeRequestState.PROGRESS) { - this.ImageKnifeRequestCallback?.showPixelMap(this.componentVersion , memoryCache.source) + this.ImageKnifeRequestCallback?.showPixelMap(this.componentVersion, memoryCache.source) + + if (requestSource == ImageKnifeRequestSource.SRC) { + this.requestState = ImageKnifeRequestState.COMPLETE + } else if (requestSource == ImageKnifeRequestSource.ERROR_HOLDER) { + this.requestState = ImageKnifeRequestState.ERROR + } } return true } diff --git a/library/src/main/ets/components/ImageKnifeComponent.ets b/library/src/main/ets/components/ImageKnifeComponent.ets index 03fad35..6e5c5c6 100644 --- a/library/src/main/ets/components/ImageKnifeComponent.ets +++ b/library/src/main/ets/components/ImageKnifeComponent.ets @@ -51,17 +51,6 @@ export struct ImageKnifeComponent { this.listener.off("layout", this.onLayoutComplete) } - aboutToReuse(params: Record): void { - if (this.request !== undefined) { - this.request.requestState = ImageKnifeRequestState.DESTROY - this.lastWidth = 0 - this.lastHeight = 0 - this.request = undefined - this.lastSrc = "" - this.componentVersion ++ - } - } - build() { Image(this.pixelMap) .objectFit(this.ImageKnifeOption.objectFit === undefined ? ImageFit.Contain : this.ImageKnifeOption.objectFit) @@ -72,10 +61,14 @@ export struct ImageKnifeComponent { } watchImageKnifeOption() { - if (this.lastSrc !== this.ImageKnifeOption.loadSrc) { + if (this.lastSrc !== this.ImageKnifeOption.loadSrc) { + if (this.request !== undefined) { + this.request.requestState = ImageKnifeRequestState.DESTROY + } this.request = undefined + this.componentVersion++ ImageKnife.getInstance().execute(this.getRequest(this.currentWidth, this.currentHeight)) - } + } } getRequest(width: number, height: number): ImageKnifeRequest { @@ -88,12 +81,11 @@ export struct ImageKnifeComponent { height, this.componentVersion, { - showPixelMap: async (version: number, pixelMap: PixelMap | string) => { - if (version !== this.componentVersion){ + showPixelMap: async (version: number, pixelMap: PixelMap | string) => { + if (version !== this.componentVersion) { return //针对reuse场景,不显示历史图片 } this.pixelMap = pixelMap - //console.info("KKKKKKKKKKK:11111111" + typeof this.pixelMap) if (typeof this.pixelMap !== 'string') { if (this.ImageKnifeOption.objectFit === ImageFit.Auto) { let info = await this.pixelMap.getImageInfo()