diff --git a/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets b/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets index 6516eb9..353d7cb 100644 --- a/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets +++ b/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets @@ -22,6 +22,7 @@ import { GIFFrame } from '../imageknife/utils/gif/GIFFrame' import { IDrawLifeCycle } from '../imageknife/interface/IDrawLifeCycle' import { LogUtil } from '../imageknife/utils/LogUtil' import { BusinessError } from '@ohos.base' +import common from '@ohos.app.ability.common' @Component export struct ImageKnifeComponent { @@ -256,6 +257,14 @@ export struct ImageKnifeComponent { } } + configHspContext(request: RequestOption){ + if(this.imageKnifeOption.context != undefined){ + request.setModuleContext(this.imageKnifeOption.context) + }else{ + request.setModuleContext(getContext(this) as common.UIAbilityContext) + } + } + configRenderGpu(request: RequestOption) { if (this.imageKnifeOption.enableGpu) { request.enableGPU() @@ -281,6 +290,7 @@ export struct ImageKnifeComponent { this.configNecessary(request); this.configCacheStrategy(request); this.configDisplay(request); + this.configHspContext(request) this.configRenderGpu(request); if(ImageKnifeGlobal.getInstance().getImageKnife()!=undefined) { ImageKnifeGlobal.getInstance().getImageKnife()?.call(request); diff --git a/imageknife/src/main/ets/components/imageknife/ImageKnifeOption.ets b/imageknife/src/main/ets/components/imageknife/ImageKnifeOption.ets index 2180386..0c4e00d 100644 --- a/imageknife/src/main/ets/components/imageknife/ImageKnifeOption.ets +++ b/imageknife/src/main/ets/components/imageknife/ImageKnifeOption.ets @@ -25,6 +25,7 @@ import { ScaleType } from '../imageknife/ImageKnifeComponent' import { rgbColor } from './transform/CropCircleWithBorderTransformation' import { RoundCorner } from './transform/RoundedCornersTransformation' import { ObjectKey } from './ObjectKey' +import common from '@ohos.app.ability.common' export interface CropCircleWithBorder{ border: number, @@ -125,7 +126,7 @@ export class ImageKnifeOption { allCacheInfoCallback?: IAllCacheInfoCallback; - + context?: common.UIAbilityContext; // sizeAnimate?: AnimateParam 由业务自定义不再支持 constructor() { diff --git a/imageknife/src/main/ets/components/imageknife/RequestOption.ets b/imageknife/src/main/ets/components/imageknife/RequestOption.ets index 5daad2b..b8fb0f2 100644 --- a/imageknife/src/main/ets/components/imageknife/RequestOption.ets +++ b/imageknife/src/main/ets/components/imageknife/RequestOption.ets @@ -49,6 +49,7 @@ import { LogUtil } from '../imageknife/utils/LogUtil' import { ImageKnifeGlobal } from './ImageKnifeGlobal' import { BusinessError } from '@ohos.base' import { ObjectKey } from './ObjectKey' +import common from '@ohos.app.ability.common' export interface Size { width: number, @@ -131,6 +132,9 @@ export class RequestOption { } } + // module资源的需要当前的module context + moduleContext:common.UIAbilityContext; + constructor() { // 初始化全局监听 this.requestListeners = new Array(); @@ -148,6 +152,14 @@ export class RequestOption { return uuid; } + setModuleContext(moduleCtx:common.UIAbilityContext){ + this.moduleContext = moduleCtx; + } + + getModuleContext():common.UIAbilityContext{ + return this.moduleContext; + } + /** * set image Component size */ diff --git a/imageknife/src/main/ets/components/imageknife/holder/ErrorHolderManager.ets b/imageknife/src/main/ets/components/imageknife/holder/ErrorHolderManager.ets index 527d447..fa3a9d4 100644 --- a/imageknife/src/main/ets/components/imageknife/holder/ErrorHolderManager.ets +++ b/imageknife/src/main/ets/components/imageknife/holder/ErrorHolderManager.ets @@ -72,7 +72,7 @@ export class ErrorHolderManager { break; } } - resourceFetch.loadResource(res, suc, onError) + resourceFetch.loadResource(this.options.getModuleContext(),res, suc, onError) } else { onError("ErrorHolderManager 输入参数有问题!") } diff --git a/imageknife/src/main/ets/components/imageknife/holder/PlaceHolderManager.ets b/imageknife/src/main/ets/components/imageknife/holder/PlaceHolderManager.ets index 612a10f..80f4c50 100644 --- a/imageknife/src/main/ets/components/imageknife/holder/PlaceHolderManager.ets +++ b/imageknife/src/main/ets/components/imageknife/holder/PlaceHolderManager.ets @@ -73,7 +73,7 @@ export class PlaceHolderManager { break; } } - resourceFetch.loadResource(res, suc, onError) + resourceFetch.loadResource(this.options.getModuleContext(),res, suc, onError) } else { onError("PlaceHolderManager 输入参数有问题!") } diff --git a/imageknife/src/main/ets/components/imageknife/holder/RetryHolderManager.ets b/imageknife/src/main/ets/components/imageknife/holder/RetryHolderManager.ets index aaf2245..f769ea6 100644 --- a/imageknife/src/main/ets/components/imageknife/holder/RetryHolderManager.ets +++ b/imageknife/src/main/ets/components/imageknife/holder/RetryHolderManager.ets @@ -73,7 +73,7 @@ export class RetryHolderManager { break; } } - resourceFetch.loadResource(res, suc, onError) + resourceFetch.loadResource(this.options.getModuleContext(), res, suc, onError) } else { onError("PlaceHolderManager 输入参数有问题!") } diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets index 6733caa..ae7d9c5 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/NetworkDownloadClient.ets @@ -43,7 +43,7 @@ export class NetworkDownloadClient implements IDataFetch { enableMetered: true, }; - loadRequest.downloadFile( (ImageKnifeGlobal.getInstance().getHapContext() as common.BaseContext ), downloadConfig).then((downloadTask:loadRequest.DownloadTask) => { + loadRequest.downloadFile( (request.getModuleContext() as common.BaseContext ), downloadConfig).then((downloadTask:loadRequest.DownloadTask) => { if (downloadTask) { let loadTask:loadRequest.DownloadTask | null = downloadTask; diff --git a/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets b/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets index 33b41d5..55d22a0 100644 --- a/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets +++ b/imageknife/src/main/ets/components/imageknife/requestmanage/RequestManager.ets @@ -241,7 +241,7 @@ export class RequestManager { } } } - this.mIResourceFetch.loadResource(request.loadSrc as Resource, success, onError); + this.mIResourceFetch.loadResource(request.getModuleContext(),request.loadSrc as Resource, success, onError); } // 加载磁盘缓存 原图 private loadDiskFromSource(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void) { @@ -323,6 +323,7 @@ export class RequestManager { width: Math.round(this.options.thumbSizeMultiplier * this.options.size.width), height: Math.round(this.options.thumbSizeMultiplier * this.options.size.height) }) + thumbOption.setModuleContext(this.options.getModuleContext()) let thumbCallback = this.options.thumbholderOnComplete; let thumbError = this.options.thumbholderOnError; this.options.transformations[0].transform(source, thumbOption,{asyncTransform: (error:BusinessError|string, pixelMap: PixelMap|null) => { @@ -539,6 +540,7 @@ export class RequestManager { thumbnailProcess(source:ArrayBuffer, filetype:string, onComplete:(value:ImageKnifeData)=>void|PromiseLike, onError:(reason?:BusinessError|string)=>void){ let thumbOption = new RequestOption(); + thumbOption.setModuleContext(this.options.getModuleContext()) thumbOption.setImageViewSize({ width: Math.round(this.options.thumbSizeMultiplier * this.options.size.width), height: Math.round(this.options.thumbSizeMultiplier * this.options.size.height) diff --git a/imageknife/src/main/ets/components/imageknife/resourcemanage/IResourceFetch.ets b/imageknife/src/main/ets/components/imageknife/resourcemanage/IResourceFetch.ets index 70057e5..1c65221 100644 --- a/imageknife/src/main/ets/components/imageknife/resourcemanage/IResourceFetch.ets +++ b/imageknife/src/main/ets/components/imageknife/resourcemanage/IResourceFetch.ets @@ -14,7 +14,9 @@ */ import { BusinessError } from '@ohos.base' +import common from '@ohos.app.ability.common'; + // 本地资源解析抽象接口 export interface IResourceFetch { - loadResource:(res: Resource, onCompleteFunction:(value:T)=>void | PromiseLike, onErrorFunction:(reason?:BusinessError|string)=>void)=>void; + loadResource:(context:common.UIAbilityContext ,res: Resource, onCompleteFunction:(value:T)=>void | PromiseLike, onErrorFunction:(reason?:BusinessError|string)=>void)=>void; } \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/resourcemanage/ParseResClient.ets b/imageknife/src/main/ets/components/imageknife/resourcemanage/ParseResClient.ets index fd27e74..6faadc2 100644 --- a/imageknife/src/main/ets/components/imageknife/resourcemanage/ParseResClient.ets +++ b/imageknife/src/main/ets/components/imageknife/resourcemanage/ParseResClient.ets @@ -20,11 +20,11 @@ import { ImageKnifeGlobal } from '../ImageKnifeGlobal'; import { BusinessError } from '@ohos.base' import common from '@ohos.app.ability.common'; export class ParseResClient implements IResourceFetch { - loadResource(res: Resource, onCompleteFunction:(value:ArrayBuffer)=>void | PromiseLike, onErrorFunction:(reason?:BusinessError|string)=>void) { + loadResource(context:common.UIAbilityContext,res: Resource, onCompleteFunction:(value:ArrayBuffer)=>void | PromiseLike, onErrorFunction:(reason?:BusinessError|string)=>void) { let resId = res.id; let resType = res.type; if (resType == ResourceTypeEts.MEDIA) { - ((ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).resourceManager as resourceManager.ResourceManager) + (context.resourceManager as resourceManager.ResourceManager) .getMediaContent(resId) .then(data => { let arrayBuffer = this.typedArrayToBuffer(data); diff --git a/imageknife/src/main/ets/components/imageknife/resourcemanage/ParseResClientBase64.ets b/imageknife/src/main/ets/components/imageknife/resourcemanage/ParseResClientBase64.ets index 6f563b5..983a982 100644 --- a/imageknife/src/main/ets/components/imageknife/resourcemanage/ParseResClientBase64.ets +++ b/imageknife/src/main/ets/components/imageknife/resourcemanage/ParseResClientBase64.ets @@ -19,14 +19,14 @@ import { Base64 } from '../../cache/Base64' import { BusinessError } from '@ohos.base' import resourceManager from '@ohos.resourceManager'; import { ImageKnifeGlobal } from '../ImageKnifeGlobal'; +import common from '@ohos.app.ability.common' export class ParseResClientBase64 implements IResourceFetch { - loadResource(res: Resource, onCompleteFunction: (value: ArrayBuffer) => void | PromiseLike, onErrorFunction: (reason?: BusinessError | string) => void) { + loadResource(context:common.UIAbilityContext,res: Resource, onCompleteFunction: (value: ArrayBuffer) => void | PromiseLike, onErrorFunction: (reason?: BusinessError | string) => void) { let resId = res.id; let resType = res.type; if (resType == ResourceTypeEts.MEDIA) { - ((ImageKnifeGlobal.getInstance() - .getHapContext() as Record).resourceManager as resourceManager.ResourceManager) + (context.resourceManager as resourceManager.ResourceManager) .getMediaContentBase64(resId) .then(data => { let matchReg = ';base64,'; diff --git a/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets b/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets index 4c04a40..20a9a2f 100644 --- a/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets +++ b/imageknife/src/main/ets/components/imageknife/transform/MaskTransformation.ets @@ -78,20 +78,20 @@ export class MaskTransformation implements BaseTransform { } imageSource.createPixelMap(options) .then(data => { - this.openInternal(data, targetWidth, targetHeight, func) + this.openInternal(request,data, targetWidth, targetHeight, func) }) .catch((e:BusinessError )=> { func?.asyncTransform(e, null); }) } - private openInternal(bitmap: PixelMap, width: number, height: number, func?: AsyncTransform) { + private openInternal(request: RequestOption,bitmap: PixelMap, width: number, height: number, func?: AsyncTransform) { if (!this._mResourceData) { if(func != undefined){ func.asyncTransform("MaskTransformation resource is empty", null) } } - let context = (ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext) + let context = (request.getModuleContext() as common.UIAbilityContext) if(context != undefined){ let resourceManager = context.resourceManager as resourceManager.ResourceManager if(resourceManager != undefined && this._mResourceData != undefined)