From 945e842e6916902083583de7b688b117cd79d4c1 Mon Sep 17 00:00:00 2001 From: zgf Date: Thu, 30 May 2024 15:19:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=BC=93=E5=AD=98=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=A2=9E=E5=8A=A0=E9=BB=98=E8=AE=A4=E5=80=BC?= =?UTF-8?q?=E3=80=81=E9=A2=84=E5=8A=A0=E8=BD=BD=E6=8E=A5=E5=8F=A3=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=BF=94=E5=9B=9E=E5=8A=A0=E8=BD=BD=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E3=80=81=E5=8A=A0=E8=BD=BD=E9=98=9F=E5=88=97?= =?UTF-8?q?=E4=BB=8E=E9=98=9F=E5=88=97=E4=BB=8EQueue=E6=94=B9=E4=B8=BAStac?= =?UTF-8?q?k=E4=BB=A5=E5=8F=8AfileType=E6=96=B0=E5=A2=9Eheic=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zgf --- CHANGELOG.md | 4 ++++ .../ohosTest/ets/test/DefaultJobQueueTest.test.ets | 8 ++++---- library/src/main/ets/ImageKnife.ets | 11 +++++++++-- library/src/main/ets/utils/DefaultJobQueue.ets | 14 +++++++------- library/src/main/ets/utils/FileTypeUtil.ets | 7 +++++-- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b869b5a..0ec29f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## 3.0.0-rc.6 - 支持多种组合变换 +- 文件缓存初始化增加默认值 +- 预加载接口新增返回加载错误信息 +- 加载队列改为使用堆Stack +- fileType图片格式新增heic格式 ## 3.0.0-rc.5 - 图片加载事件增加请求开始的回调,以及修复有缓存时,没有回调的bug diff --git a/entry/src/ohosTest/ets/test/DefaultJobQueueTest.test.ets b/entry/src/ohosTest/ets/test/DefaultJobQueueTest.test.ets index 7816a73..eb13a71 100644 --- a/entry/src/ohosTest/ets/test/DefaultJobQueueTest.test.ets +++ b/entry/src/ohosTest/ets/test/DefaultJobQueueTest.test.ets @@ -55,12 +55,12 @@ export default function DefaultJobQueueTest() { expect(job.getQueueLength()).assertEqual(7) expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("high1") - expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("medium1") - expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("medium2") - expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("medium3") expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("medium4") - expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("low1") + expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("medium3") + expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("medium2") + expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("medium1") expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("low2") + expect(job.pop()!.imageKnifeOption.loadSrc).assertEqual("low1") expect(job.pop()).assertEqual(undefined) expect(job.getQueueLength()).assertEqual(0) diff --git a/library/src/main/ets/ImageKnife.ets b/library/src/main/ets/ImageKnife.ets index 5dbe14b..d33d65c 100644 --- a/library/src/main/ets/ImageKnife.ets +++ b/library/src/main/ets/ImageKnife.ets @@ -53,7 +53,7 @@ export class ImageKnife { * @param size 缓存数量 * @param memory 内存大小 */ - async initFileCache(context: Context, size: number, memory: number) { + async initFileCache(context: Context, size: number = 256, memory: number = 256 * 1024 * 1024) { this.fileCache = new FileCache(context, size, memory) await this.fileCache.initFileCache() } @@ -163,6 +163,14 @@ export class ImageKnife { let fileKey = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature) let cachePath = ImageKnife.getInstance().getFileCache().getFileToPath(fileKey) if (cachePath == null || cachePath == "" || cachePath == undefined) { + imageKnifeOption.onLoadListener = { + onLoadSuccess(){ + resolve(ImageKnife.getInstance().getFileCache().getFileToPath(fileKey)) + }, + onLoadFailed(err) { + reject(err) + } + } let request = new ImageKnifeRequest( imageKnifeOption, imageKnifeOption.context !== undefined ? imageKnifeOption.context : getContext(this) as common.UIAbilityContext, @@ -171,7 +179,6 @@ export class ImageKnife { 0, { showPixelMap(version: number, pixelMap: PixelMap | string) { - resolve(ImageKnife.getInstance().getFileCache().getFileToPath(fileKey)) } } ) diff --git a/library/src/main/ets/utils/DefaultJobQueue.ets b/library/src/main/ets/utils/DefaultJobQueue.ets index eecf735..ac31956 100644 --- a/library/src/main/ets/utils/DefaultJobQueue.ets +++ b/library/src/main/ets/utils/DefaultJobQueue.ets @@ -15,12 +15,12 @@ import { ImageKnifeRequest } from '../ImageKnifeRequest'; import { IJobQueue } from './IJobQueue' import Queue from '@ohos.util.Queue'; -import { taskpool } from '@kit.ArkTS'; +import { taskpool,Stack } from '@kit.ArkTS'; export class DefaultJobQueue implements IJobQueue { - highQueue: Queue = new Queue(); - normalQueue: Queue = new Queue(); - lowQueue: Queue = new Queue(); + highQueue: Stack = new Stack(); + normalQueue: Stack = new Stack(); + lowQueue: Stack = new Stack(); getQueueLength(): number { return this.highQueue.length + this.normalQueue.length + this.lowQueue.length @@ -28,11 +28,11 @@ export class DefaultJobQueue implements IJobQueue { add(request: ImageKnifeRequest): void { if (request.imageKnifeOption.priority === undefined || request.imageKnifeOption.priority === taskpool.Priority.MEDIUM) { - this.normalQueue.add(request) + this.normalQueue.push(request) } else if (request.imageKnifeOption.priority === taskpool.Priority.HIGH) { - this.highQueue.add(request) + this.highQueue.push(request) } else { - this.lowQueue.add(request) + this.lowQueue.push(request) } } diff --git a/library/src/main/ets/utils/FileTypeUtil.ets b/library/src/main/ets/utils/FileTypeUtil.ets index c87c939..ee834c4 100644 --- a/library/src/main/ets/utils/FileTypeUtil.ets +++ b/library/src/main/ets/utils/FileTypeUtil.ets @@ -25,6 +25,7 @@ export class FileTypeUtil { 'webp': [new Uint8Array([0x52, 0x49, 0x46, 0x46])], 'tiff': [new Uint8Array([0x49, 0x20, 0x49]), new Uint8Array([0x49, 0x49, 0x2A, 0x00]), new Uint8Array([0x4D, 0x4D, 0x00, 0x2A]), new Uint8Array([0x4D, 0x4D, 0x00, 0x2B])], // 添加更多的文件类型和特征 + 'heic': [new Uint8Array([0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00])], }; @@ -40,7 +41,8 @@ export class FileTypeUtil { value == SupportFormat.webp || value == SupportFormat.bmp || value == SupportFormat.gif || - value == SupportFormat.svg + value == SupportFormat.svg || + value == SupportFormat.heic ) { return true; } @@ -105,5 +107,6 @@ export enum SupportFormat { bmp = 'bmp', gif = 'gif', svg = 'svg', - tiff = 'tiff' + tiff = 'tiff', + heic = 'heic' }