diff --git a/imageknife/src/main/ets/components/imageknife/RequestOption.ets b/imageknife/src/main/ets/components/imageknife/RequestOption.ets index abdeace..72682c1 100644 --- a/imageknife/src/main/ets/components/imageknife/RequestOption.ets +++ b/imageknife/src/main/ets/components/imageknife/RequestOption.ets @@ -47,7 +47,11 @@ import { ToonFilterTransform } from '../imageknife/transform/ToonFilterTransform import { VignetteFilterTransform } from '../imageknife/transform/VignetteFilterTransform' import { LogUtil } from '../imageknife/utils/LogUtil' import { ImageKnifeGlobal } from './ImageKnifeGlobal' - +import { BusinessError } from '@ohos.base' +export interface Size { + width: number, + height: number +} export class RequestOption { loadSrc: string | PixelMap | Resource; strategy: DiskStrategy = new AUTOMATIC(); @@ -119,10 +123,7 @@ export class RequestOption { /** * set image Component size */ - setImageViewSize(imageSize: { - width: number, - height: number - }) { + setImageViewSize(imageSize:Size) { this.size.width = imageSize.width; this.size.height = imageSize.height; return this; @@ -386,7 +387,7 @@ export class RequestOption { // 缩略图解析成功 - thumbholderOnComplete(imageKnifeData: ImageKnifeData) { + thumbholderOnComplete = (imageKnifeData: ImageKnifeData)=> { if (!this.loadMainReady && !(this.loadErrorReady || this.loadRetryReady)) { //主图未加载成功,并且未加载失败 显示占位图 主图加载成功或者加载失败后=>不展示占位图 this.thumbHolderFunc.asyncSuccess(imageKnifeData) @@ -394,7 +395,7 @@ export class RequestOption { } // 缩略图解析失败 - thumbholderOnError(error) { + thumbholderOnError=(error? :BusinessError|string)=>{ LogUtil.log("缩略图解析失败 error =" + error) } @@ -423,7 +424,7 @@ export class RequestOption { LogUtil.log("重试占位图解析失败 error =" + error) } - loadComplete(imageKnifeData: ImageKnifeData) { + loadComplete = (imageKnifeData: ImageKnifeData)=>{ this.loadMainReady = true; // 三级缓存数据加载成功 for (let requestListener of this.requestListeners) { @@ -445,11 +446,11 @@ export class RequestOption { } // 图片文件落盘之后会自动去寻找下一个数据加载 - removeCurrentAndSearchNext(){ + removeCurrentAndSearchNext =()=>{ (ImageKnifeGlobal.getInstance().getImageKnife()).removeRunning(this); } - loadError(err) { + loadError = (err:BusinessError|string)=>{ LogUtil.log("loadError:" + err); //失败占位图展示规则 if (this.retryholderFunc) { diff --git a/imageknife/src/main/ets/components/imageknife/interface/IAllCacheInfoCallback.ets b/imageknife/src/main/ets/components/imageknife/interface/IAllCacheInfoCallback.ets index 4e6c7d0..38e62ef 100644 --- a/imageknife/src/main/ets/components/imageknife/interface/IAllCacheInfoCallback.ets +++ b/imageknife/src/main/ets/components/imageknife/interface/IAllCacheInfoCallback.ets @@ -16,7 +16,7 @@ import {ImageKnifeData} from "../../imageknife/ImageKnifeData" export interface MemoryCacheInfo{ key: string, - data: ImageKnifeData + data: ImageKnifeData | undefined } export interface ResourceCacheInfo{ path: string, @@ -27,9 +27,9 @@ export interface DataCacheInfo{ key: string } export interface AllCacheInfo { - memoryCacheInfo: MemoryCacheInfo - resourceCacheInfo: ResourceCacheInfo - dataCacheInfo: DataCacheInfo + memoryCacheInfo: MemoryCacheInfo | undefined + resourceCacheInfo: ResourceCacheInfo | undefined + dataCacheInfo: DataCacheInfo | undefined } export interface IAllCacheInfoCallback { diff --git a/imageknife/src/main/ets/components/imageknife/requestmanage/DiskCacheProxy.ets b/imageknife/src/main/ets/components/imageknife/requestmanage/DiskCacheProxy.ets index 51087e6..e02f161 100644 --- a/imageknife/src/main/ets/components/imageknife/requestmanage/DiskCacheProxy.ets +++ b/imageknife/src/main/ets/components/imageknife/requestmanage/DiskCacheProxy.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -import type { ICache } from "../requestmanage/ICache" +import { ICache } from "../requestmanage/ICache" import { DiskLruCache } from "@ohos/disklrucache" export class DiskCacheProxy implements ICache { @@ -47,7 +47,7 @@ export class DiskCacheProxy implements ICache { removeValue(key: string): ArrayBuffer{ // Disk暂无实现 - return; + return new ArrayBuffer(0); } clear() { diff --git a/imageknife/src/main/ets/components/imageknife/requestmanage/ICache.ets b/imageknife/src/main/ets/components/imageknife/requestmanage/ICache.ets index a108f84..59e5b74 100644 --- a/imageknife/src/main/ets/components/imageknife/requestmanage/ICache.ets +++ b/imageknife/src/main/ets/components/imageknife/requestmanage/ICache.ets @@ -18,11 +18,11 @@ export interface ICache { // 缓存类型 getName(): string - getValue(key: K): V; + getValue(key: K): V|undefined; putValue(key: K, value: V); - removeValue(key: K): V; + removeValue(key: K): V|undefined; clear(); diff --git a/imageknife/src/main/ets/components/imageknife/requestmanage/MemoryCacheProxy.ets b/imageknife/src/main/ets/components/imageknife/requestmanage/MemoryCacheProxy.ets index 13bab96..889cc53 100644 --- a/imageknife/src/main/ets/components/imageknife/requestmanage/MemoryCacheProxy.ets +++ b/imageknife/src/main/ets/components/imageknife/requestmanage/MemoryCacheProxy.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -import type {ICache} from "../requestmanage/ICache" +import {ICache} from "../requestmanage/ICache" import {LruCache} from "../../cache/LruCache" export class MemoryCacheProxy implements ICache { diff --git a/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets b/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets index f96ac23..4229ef2 100644 --- a/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets +++ b/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets @@ -13,28 +13,26 @@ * limitations under the License. */ -import { RequestOption } from '../../imageknife/RequestOption' +import { RequestOption,Size } from '../../imageknife/RequestOption' import { DiskLruCache } from '@ohos/disklrucache' import { LruCache } from '../../cache/LruCache' import { SparkMD5 } from '../../3rd_party/sparkmd5/spark-md5' import { MemoryCacheProxy } from '../requestmanage/MemoryCacheProxy' import { DiskCacheProxy } from '../requestmanage/DiskCacheProxy' import { FileTypeUtil } from '../utils/FileTypeUtil' -import type { IDataFetch } from '../../imageknife/networkmanage/IDataFetch' -import type { IResourceFetch } from '../../imageknife/resourcemanage/IResourceFetch' +import { IDataFetch } from '../../imageknife/networkmanage/IDataFetch' +import { IResourceFetch } from '../../imageknife/resourcemanage/IResourceFetch' import { ImageKnifeData, ImageKnifeType } from '../ImageKnifeData' import { AllCacheInfo } from '../../imageknife/interface/IAllCacheInfoCallback' import { ParseImageUtil } from '../utils/ParseImageUtil' -import type { IParseImage } from '../interface/IParseImage' +import { IParseImage } from '../interface/IParseImage' import image from '@ohos.multimedia.image' import { SVGParseImpl } from '../utils/svg/SVGParseImpl' import { GIFParseImpl } from '../utils/gif/GIFParseImpl' import { GIFFrame } from '../utils/gif/GIFFrame' import { LogUtil } from '../../imageknife/utils/LogUtil' +import { BusinessError } from '@ohos.base' -export interface AsyncString { - (data: string): void; -} export enum Stage { @@ -64,13 +62,13 @@ export enum RunReason { export class RequestManager { private TAG: string = "RequestManager"; private options: RequestOption; - private mMemoryCacheProxy: MemoryCacheProxy; + private mMemoryCacheProxy: MemoryCacheProxy; private mDiskCacheProxy: DiskCacheProxy; private mIDataFetch: IDataFetch; private mIResourceFetch: IResourceFetch; - private mParseImageUtil: IParseImage; + private mParseImageUtil: IParseImage; - constructor(option: RequestOption, memoryCache1: LruCache, diskMemoryCache1: DiskLruCache, dataFetch: IDataFetch, resourceFetch: IResourceFetch) { + constructor(option: RequestOption, memoryCache1: LruCache, diskMemoryCache1: DiskLruCache, dataFetch: IDataFetch, resourceFetch: IResourceFetch) { this.options = option; // 缓存部分 @@ -87,23 +85,27 @@ export class RequestManager { this.mParseImageUtil = new ParseImageUtil(); } - static execute(option: RequestOption, memoryCache1: LruCache, diskMemoryCache1: DiskLruCache, dataFetch: IDataFetch, resourceFetch: IResourceFetch) { + static execute(option: RequestOption, memoryCache1: LruCache, diskMemoryCache1: DiskLruCache, dataFetch: IDataFetch, resourceFetch: IResourceFetch) { LogUtil.log("RequestManager execute") let manager = new RequestManager(option, memoryCache1, diskMemoryCache1, dataFetch, resourceFetch); - return new Promise(manager.process.bind(manager)) - .then(option.loadComplete.bind(option)) - .then(manager.loadCompleteAfter.bind(manager)) - .catch(option.loadError.bind(option)); + return new Promise(manager.process) + .then(option.loadComplete) + .then(manager.loadCompleteAfter) + .catch(option.loadError); } - loadCompleteAfter() { + loadCompleteAfter =()=>{ try { // 内部消化问题 LogUtil.log("loadCompleteAfter!") if (this.options.allCacheInfoCallback) { LogUtil.log("RequestOption =" + JSON.stringify(this.options)); // 内存缓存 - let allCacheInfo = new AllCacheInfo(); + let allCacheInfo:AllCacheInfo = { + memoryCacheInfo:{key:'', data:new ImageKnifeData()}, + resourceCacheInfo:{key:'', path:''}, + dataCacheInfo:{key:'',path:''} + }; let memoryCache = this.mMemoryCacheProxy.getValue(this.options.generateCacheKey); allCacheInfo.memoryCacheInfo = { key: this.options.generateCacheKey, @@ -133,12 +135,12 @@ export class RequestManager { private mStage: Stage = Stage.INITIALIZE; private mRunReason: RunReason = RunReason.INITIALIZE; - process(onComplete, onError) { + process = (onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void)=>{ LogUtil.log("RequestManager process !"); this.loadLeve1MemoryCache(onComplete, onError) } - private runWrapped(request: RequestOption, runReason: RunReason, onComplete, onError) { + private runWrapped(request: RequestOption, runReason: RunReason, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { LogUtil.log("RequestManager runWrapped") if (runReason == RunReason.INITIALIZE) { this.mStage = this.getNextStage(request, this.mStage); @@ -169,7 +171,7 @@ export class RequestManager { } //究竟从哪里加载数据 - private searchLoadFrom(request: RequestOption, current: Stage, onComplete, onError) { + private searchLoadFrom(request: RequestOption, current: Stage, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { LogUtil.log("RequestManager searchLoadFrom") if (current == Stage.RESOURCE_CACHE) { this.loadDiskFromTransform(request, onComplete, onError); @@ -185,8 +187,8 @@ export class RequestManager { } // 加载网络资源 - private loadSourceFromNetwork(request: RequestOption, onComplete, onError) { - let success = (arraybuffer) => { + private loadSourceFromNetwork(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { + let success = (arraybuffer:ArrayBuffer) => { this.downloadSuccess(arraybuffer, onComplete, onError) } let error = (errorMsg:string) =>{ @@ -196,10 +198,10 @@ export class RequestManager { } // 加载本地资源 - private loadSourceFormNative(request: RequestOption, onComplete, onError) { + private loadSourceFormNative(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { LogUtil.log("RequestManager loadSourceFormNative") // 本地解析后进行一级缓存 - let success = (arrayBuffer) => { + let success = (arrayBuffer:ArrayBuffer) => { // 使用媒体子系统 ImageSource解析文件 获取PixelMap let fileTypeUtil = new FileTypeUtil(); let typeValue = fileTypeUtil.getFileType(arrayBuffer) @@ -217,7 +219,7 @@ export class RequestManager { }) } else { if (request.transformations[0]) { - request.transformations[0].transform(arrayBuffer, request, (error, pixelMap: PixelMap) => { + request.transformations[0].transform(arrayBuffer, request, {asyncTransform:(error:BusinessError|string, pixelMap: PixelMap) => { // 输出给Image if (pixelMap) { @@ -227,7 +229,7 @@ export class RequestManager { } else { onError(error); } - }) + }}) } else { let success = (value: PixelMap) => { @@ -242,7 +244,7 @@ export class RequestManager { this.mIResourceFetch.loadResource(request.loadSrc as Resource, success, onError); } // 加载磁盘缓存 原图 - private loadDiskFromSource(request: RequestOption, onComplete, onError) { + private loadDiskFromSource(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { LogUtil.log("RequestManager loadDiskFromSource") let cached = this.mDiskCacheProxy.getValue(request.generateDataKey) if (cached != null && cached.byteLength > 0) { @@ -254,7 +256,7 @@ export class RequestManager { } // 加载磁盘缓存 变换后图片 - private loadDiskFromTransform(request: RequestOption, onComplete, onError) { + private loadDiskFromTransform(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { LogUtil.log("RequestManager loadDiskFromTransform") let cached = this.mDiskCacheProxy.getValue(request.generateResourceKey) if (cached != null) { @@ -265,7 +267,7 @@ export class RequestManager { } } - parseSource(request: RequestOption, onComplete, onError) { + parseSource(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { LogUtil.log("RequestManager parseSource") if ((typeof (request.loadSrc as image.PixelMap).isEditable) == 'boolean') { // PixelMap 外层捕获效率更高,不会进入这里 @@ -282,7 +284,7 @@ export class RequestManager { } - private loadLeve1MemoryCache(onComplete, onError) { + private loadLeve1MemoryCache(onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { LogUtil.log("RequestManager loadLeve1MemoryCache") // 一级缓存 内存获取 let cache = this.mMemoryCacheProxy.loadMemoryCache(this.options.generateCacheKey, this.options.isCacheable); @@ -297,7 +299,7 @@ export class RequestManager { } // 解析磁盘文件变成PixeMap - private parseDiskFile2PixelMap(request: RequestOption, source: ArrayBuffer, onComplete, onError) { + private parseDiskFile2PixelMap(request: RequestOption, source: ArrayBuffer, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { // 步骤一:文件转为pixelMap 然后变换 给Image组件 let fileTypeUtil = new FileTypeUtil(); let typeValue = fileTypeUtil.getFileType(source); @@ -314,23 +316,23 @@ export class RequestManager { } else { if (this.options.transformations[0]) { if (this.options.thumbSizeMultiplier) { - let thumbOption = new RequestOption(); + let thumbOption:RequestOption = new RequestOption(); thumbOption.setImageViewSize({ width: Math.round(this.options.thumbSizeMultiplier * this.options.size.width), height: Math.round(this.options.thumbSizeMultiplier * this.options.size.height) }) - let thumbCallback = this.options.thumbholderOnComplete.bind(this.options); - let thumbError = this.options.thumbholderOnError.bind(this.options); - this.options.transformations[0].transform(source, thumbOption, (error, pixelMap: PixelMap) => { + let thumbCallback = this.options.thumbholderOnComplete; + let thumbError = this.options.thumbholderOnError; + this.options.transformations[0].transform(source, thumbOption,{asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => { if (pixelMap) { let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap); thumbCallback(imageKnifeData); } else { thumbError(error); } - }) + }}) setTimeout(()=>{ - this.options.transformations[0].transform(source, request, (error, pixelMap: PixelMap) => { + this.options.transformations[0].transform(source, request, {asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => { if (pixelMap) { // 保存一份变换后的图片PixelMap到MemoryCache let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap); @@ -339,11 +341,11 @@ export class RequestManager { } else { onError(error); } - }) + }}) },this.options.thumbDelayTime); } else { - this.options.transformations[0].transform(source, request, (error, pixelMap: PixelMap) => { + this.options.transformations[0].transform(source, request, {asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => { if (pixelMap) { // 保存一份变换后的图片PixelMap到MemoryCache let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap); @@ -352,13 +354,13 @@ export class RequestManager { } else { onError(error); } - }) + }}) } } else { // thumbnail 缩略图部分 if (request.thumbSizeMultiplier) { - let thumbCallback = this.options.thumbholderOnComplete.bind(this.options); - let thumbError = this.options.thumbholderOnError.bind(this.options); + let thumbCallback = this.options.thumbholderOnComplete + let thumbError = this.options.thumbholderOnError let thumbSuccess = (value: PixelMap) => { let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value); thumbCallback(imageKnifeData); @@ -386,13 +388,13 @@ export class RequestManager { } // 解析磁盘变换后文件变成PixeMap - private parseDiskTransformFile2PixelMap(request: RequestOption, source: ArrayBuffer, onComplete, onError) { + private parseDiskTransformFile2PixelMap(request: RequestOption, source: ArrayBuffer, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { let fileTypeUtil = new FileTypeUtil(); let typeValue = fileTypeUtil.getFileType(source); // thumbnail 缩略图部分 if (request.thumbSizeMultiplier) { - let thumbCallback = this.options.thumbholderOnComplete.bind(this.options); - let thumbError = this.options.thumbholderOnError.bind(this.options); + let thumbCallback = this.options.thumbholderOnComplete + let thumbError = this.options.thumbholderOnError let thumbSuccess = (value: PixelMap) => { let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value); thumbCallback(imageKnifeData); @@ -416,7 +418,7 @@ export class RequestManager { } } - private downloadSuccess(source: ArrayBuffer, onComplete, onError) { + private downloadSuccess(source: ArrayBuffer, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { LogUtil.log('Download task completed.'); if(source == null || source == undefined || source.byteLength <= 0){ @@ -447,8 +449,8 @@ export class RequestManager { .then(async (arraybuffer: ArrayBuffer)=>{ await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer) }) - .catch(err=>{ - LogUtil.log('download file is ='+ImageKnifeData.GIF+'and save diskLruCache error ='+ err) + .catch( (err:BusinessError)=>{ + LogUtil.log('download file is ='+ImageKnifeData.GIF+'and save diskLruCache error ='+ (err as BusinessError)) }) }else if(ImageKnifeData.SVG == filetype){ // 处理svg @@ -461,8 +463,8 @@ export class RequestManager { .then(async (arraybuffer: ArrayBuffer)=>{ await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer) }) - .catch(err=>{ - LogUtil.log('download file is ='+ImageKnifeData.SVG+'and save diskLruCache error ='+ err) + .catch((err:BusinessError)=>{ + LogUtil.log('download file is ='+ImageKnifeData.SVG+'and save diskLruCache error ='+ (err as BusinessError)) }) } else { // 进行变换 @@ -471,19 +473,19 @@ export class RequestManager { if (this.options.thumbSizeMultiplier) { this.thumbnailProcess(source, filetype, onComplete, onError); } else { - this.options.transformations[0].transform(source, this.options, (error, pixelMap: PixelMap) => { + this.options.transformations[0].transform(source, this.options, {asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => { if (pixelMap) { this.saveCacheAndDisk(pixelMap, filetype, onComplete, source); } else { onError(error); } - }) + }}) } } else { // thumbnail 缩略图部分 if (this.options.thumbSizeMultiplier) { - let thumbCallback = this.options.thumbholderOnComplete.bind(this.options); - let thumbError = this.options.thumbholderOnError.bind(this.options); + let thumbCallback = this.options.thumbholderOnComplete + let thumbError = this.options.thumbholderOnError let thumbSuccess = (value: PixelMap) => { let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value); thumbCallback(imageKnifeData); @@ -515,16 +517,16 @@ export class RequestManager { - private saveCacheAndDisk(value: PixelMap, filetype:string, onComplete, source:ArrayBuffer) { + private saveCacheAndDisk(value: PixelMap, filetype:string, onComplete:(value:ImageKnifeData)=>void|PromiseLike, source:ArrayBuffer) { let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value); this.mMemoryCacheProxy.putValue(this.options.generateCacheKey, imageKnifeData); - let save2DiskCache = async (arraybuffer) => { + let save2DiskCache = async (arraybuffer:ArrayBuffer) => { await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer) // 落盘之后需要主动移除当前request并且调用下一个加载 - let removeCurrentAndSearchNextRun = this.options.removeCurrentAndSearchNext.bind(this.options) + let removeCurrentAndSearchNextRun = this.options.removeCurrentAndSearchNext removeCurrentAndSearchNextRun(); } - let runSave2Disk = (resolve, reject) => { + let runSave2Disk = (resolve:(value:ArrayBuffer)=>void|PromiseLike, reject:(reason?:BusinessError|string)=>void) => { resolve(source); } let promise = new Promise(runSave2Disk); @@ -533,49 +535,49 @@ export class RequestManager { onComplete(imageKnifeData); } - thumbnailProcess(source:ArrayBuffer, filetype:string, onComplete, onError){ + thumbnailProcess(source:ArrayBuffer, filetype:string, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void){ let thumbOption = new RequestOption(); thumbOption.setImageViewSize({ width: Math.round(this.options.thumbSizeMultiplier * this.options.size.width), height: Math.round(this.options.thumbSizeMultiplier * this.options.size.height) }) - let thumbCallback = this.options.thumbholderOnComplete.bind(this.options); - let thumbError = this.options.thumbholderOnError.bind(this.options); - this.options.transformations[0].transform(source, thumbOption, (error, pixelMap: PixelMap) => { + let thumbCallback = this.options.thumbholderOnComplete + let thumbError = this.options.thumbholderOnError + this.options.transformations[0].transform(source, thumbOption, {asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => { if (pixelMap) { let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap); thumbCallback(imageKnifeData); } else { thumbError(error); } - }) + }}) setTimeout(() => { - this.options.transformations[0].transform(source, this.options, (error, pixelMap: PixelMap) => { + this.options.transformations[0].transform(source, this.options,{asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => { if (pixelMap) { this.saveCacheAndDisk(pixelMap, filetype, onComplete, source); } else { onError(error); } - }) + }}) }, this.options.thumbDelayTime) } - private svgProcess(onComplete, onError, arraybuffer, typeValue, cacheStrategy?: (cacheData: ImageKnifeData) => void) { + private svgProcess(onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void, arraybuffer:ArrayBuffer, typeValue:string, cacheStrategy?: (cacheData: ImageKnifeData) => void) { let svgParseImpl = new SVGParseImpl() - let size = { width: this.options.size.width, height: this.options.size.height } + let size:Size = { width: this.options.size.width, height: this.options.size.height } svgParseImpl.parseSvg(arraybuffer, size).then((value: PixelMap) => { let imageKnifeData = ImageKnifeData.createImagePixelMap(ImageKnifeType.PIXELMAP, value) if(cacheStrategy){ cacheStrategy(imageKnifeData) } onComplete(imageKnifeData) - }).catch(err => { + }).catch((err:BusinessError) => { onError(err) }) } - private gifProcess(onComplete, onError, arraybuffer, typeValue, cacheStrategy?: (cacheData: ImageKnifeData) => void) { + private gifProcess(onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void, arraybuffer:ArrayBuffer, typeValue:string, cacheStrategy?: (cacheData: ImageKnifeData) => void) { let gifParseImpl = new GIFParseImpl() - gifParseImpl.parseGifs(arraybuffer, (data?,err?)=>{ + gifParseImpl.parseGifs(arraybuffer, (data?:GIFFrame[],err?:BusinessError|string)=>{ if(err){ onError(err) } diff --git a/imageknife/src/main/ets/components/imageknife/transform/AsyncTransform.ets b/imageknife/src/main/ets/components/imageknife/transform/AsyncTransform.ets index 80bfd74..a868d1f 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/AsyncTransform.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/AsyncTransform.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +import { BusinessError } from '@ohos.base' export interface AsyncTransform { - (err, data: T) + asyncTransform:(err:BusinessError|string, data: T)=>void } diff --git a/imageknife/src/main/ets/components/imageknife/utils/gif/GIFParseImpl.ets b/imageknife/src/main/ets/components/imageknife/utils/gif/GIFParseImpl.ets index aecbc66..bf2b01d 100644 --- a/imageknife/src/main/ets/components/imageknife/utils/gif/GIFParseImpl.ets +++ b/imageknife/src/main/ets/components/imageknife/utils/gif/GIFParseImpl.ets @@ -19,9 +19,11 @@ import { parseBufferToFrame } from './parse/GIFParse' import { LogUtil } from '../../utils/LogUtil' import image from '@ohos.multimedia.image' import { ImageKnifeGlobal } from '../../ImageKnifeGlobal' +import { BusinessError } from '@ohos.base' +import worker from '@ohos.worker'; export class GIFParseImpl implements IParseGif { - parseGifs(imageinfo: ArrayBuffer, callback: (data?, err?) => void, worker?,runMainThread?:boolean) { + parseGifs(imageinfo: ArrayBuffer, callback: (data?:GIFFrame[], err?:BusinessError|string) => void, worker?:worker.ThreadWorker,runMainThread?:boolean) { let resolveWorker = worker; LogUtil.log('parseGifs resolveWorker1 is null =' + (resolveWorker == null)) if (!resolveWorker) { diff --git a/imageknife/src/main/ets/components/imageknife/utils/gif/IParseGif.ets b/imageknife/src/main/ets/components/imageknife/utils/gif/IParseGif.ets index 19ff54b..d80f388 100644 --- a/imageknife/src/main/ets/components/imageknife/utils/gif/IParseGif.ets +++ b/imageknife/src/main/ets/components/imageknife/utils/gif/IParseGif.ets @@ -12,7 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { BusinessError } from '@ohos.base' +import { GIFFrame } from './GIFFrame' +import worker from '@ohos.worker'; export interface IParseGif{ // gif解析 - parseGifs(imageinfo:ArrayBuffer,callback:(data?,err?)=>void,worker?) + parseGifs:(imageinfo: ArrayBuffer, callback: (data?:GIFFrame[], err?:BusinessError|string) => void, worker?:worker.ThreadWorker,runMainThread?:boolean)=>void } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/utils/svg/SVGParseImpl.ets b/imageknife/src/main/ets/components/imageknife/utils/svg/SVGParseImpl.ets index 6d57f64..9ffbe7d 100644 --- a/imageknife/src/main/ets/components/imageknife/utils/svg/SVGParseImpl.ets +++ b/imageknife/src/main/ets/components/imageknife/utils/svg/SVGParseImpl.ets @@ -12,11 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import type {IParseSvg} from'./IParseSvg' +import {IParseSvg} from'./IParseSvg' import {SVGImageView} from '@ohos/svg' -export interface Size{ - width:number,height:number -} +import {Size} from '../../RequestOption' + export class SVGParseImpl implements IParseSvg{ parseSvg(imageinfo: ArrayBuffer,size?:Size): Promise{ let model = new SVGImageView.SVGImageViewModel();