1.修复新版本图片错位显示问题:通过watchoption变化后发起请求

2.修复展位图错误图,没有从缓存拿取的问题
3.修复请求超过并发数,放队列后仍然直接下发请求的问题

Signed-off-by: madixin <madixin@huawei.com>
This commit is contained in:
madixin 2024-04-08 08:41:15 +08:00
parent 07dc0b0ca4
commit 46b12bf7c2
6 changed files with 35 additions and 40 deletions

7
.gitignore vendored
View File

@ -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

View File

@ -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"
}
}
}

View File

@ -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)) {

View File

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

View File

@ -51,17 +51,6 @@ export struct ImageKnifeComponent {
this.listener.off("layout", this.onLayoutComplete)
}
aboutToReuse(params: Record<string, ESObject>): 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()