文件缓存初始化增加默认值、预加载接口新增返回加载错误信息、加载队列从队列从Queue改为Stack以及fileType新增heic格式

Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
zgf 2024-05-30 15:19:54 +08:00
parent 52e5d1f5bf
commit 945e842e69
5 changed files with 29 additions and 15 deletions

View File

@ -1,5 +1,9 @@
## 3.0.0-rc.6
- 支持多种组合变换
- 文件缓存初始化增加默认值
- 预加载接口新增返回加载错误信息
- 加载队列改为使用堆Stack
- fileType图片格式新增heic格式
## 3.0.0-rc.5
- 图片加载事件增加请求开始的回调,以及修复有缓存时没有回调的bug

View File

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

View File

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

View File

@ -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<ImageKnifeRequest> = new Queue();
normalQueue: Queue<ImageKnifeRequest> = new Queue();
lowQueue: Queue<ImageKnifeRequest> = new Queue();
highQueue: Stack<ImageKnifeRequest> = new Stack();
normalQueue: Stack<ImageKnifeRequest> = new Stack();
lowQueue: Stack<ImageKnifeRequest> = 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)
}
}

View File

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