forked from floraachy/ImageKnife
更新说明:
1、moduleContext新增缓存策略,缓存上限5,缓存策略Lru Signed-off-by: 明月清风 <qiufeihu1@h-partners.com>
This commit is contained in:
parent
ca7a8c2fea
commit
ea87330bb3
|
@ -1,3 +1,6 @@
|
|||
## 2.1.2-rc.5
|
||||
- moduleContext新增缓存策略,缓存上限5,缓存策略Lru
|
||||
|
||||
## 2.1.2-rc.4
|
||||
- canvas新增抗锯齿
|
||||
- 修复图片缩放时出现重影
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
"main": "index.ets",
|
||||
"repository": "https://gitee.com/openharmony-tpc/ImageKnife",
|
||||
"type": "module",
|
||||
"version": "2.1.2-rc.4",
|
||||
"version": "2.1.2-rc.5",
|
||||
"dependencies": {
|
||||
"pako": "^2.1.0",
|
||||
"@ohos/disklrucache": "^2.0.2-rc.0",
|
||||
|
|
|
@ -62,6 +62,11 @@ export class LruCache <K, V> {
|
|||
return preValue
|
||||
}
|
||||
|
||||
// 是否存在key
|
||||
has(key: K): boolean {
|
||||
return this.map.hasKey(key);
|
||||
}
|
||||
|
||||
/*
|
||||
* 替换或删除对应key键的数据
|
||||
* evicted:是否删除
|
||||
|
@ -135,5 +140,4 @@ export class LruCache <K, V> {
|
|||
foreachLruCache(fn: (value: V, key: K, map: Map<K, V>) => void) {
|
||||
this.map.each(fn);
|
||||
}
|
||||
|
||||
}
|
|
@ -20,6 +20,7 @@ import { ImageKnifeGlobal } from '../../ImageKnifeGlobal';
|
|||
import resourceManager from '@ohos.resourceManager';
|
||||
import { BusinessError } from '@ohos.base'
|
||||
import common from '@ohos.app.ability.common';
|
||||
import { ContextCacheProxy } from '../../requestmanage/ContextCacheProxy';
|
||||
|
||||
export class RecourseProvider extends CompressAdapter {
|
||||
private static CHARS: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||
|
@ -46,8 +47,11 @@ export class RecourseProvider extends CompressAdapter {
|
|||
if (!this._mResourceData) {
|
||||
throw Error("compress resource is empty");
|
||||
}
|
||||
((ImageKnifeGlobal.getInstance()
|
||||
.getHapContext() as common.UIAbilityContext).createModuleContext(this._mResourceData.moduleName).resourceManager as resourceManager.ResourceManager)
|
||||
let context = ContextCacheProxy.getInstance()
|
||||
.contextGetValue(this._mResourceData.moduleName,
|
||||
ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext);
|
||||
if (context != undefined) {
|
||||
(context.resourceManager as resourceManager.ResourceManager)
|
||||
.getMediaContent(this._mResourceData.id)
|
||||
.then(data => {
|
||||
let buffer = this.uint8ArrayToBuffer(data);
|
||||
|
@ -61,7 +65,7 @@ export class RecourseProvider extends CompressAdapter {
|
|||
.catch((err: BusinessError) => {
|
||||
console.log("RecourseProvider openInternal err" + JSON.stringify(err as BusinessError));
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
getPixelMapFormat(): PixelMapFormat | undefined {
|
||||
|
@ -70,6 +74,7 @@ export class RecourseProvider extends CompressAdapter {
|
|||
}
|
||||
return this.getFormat(this._mPixelMapHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* data decode
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ICache } from "../requestmanage/ICache"
|
||||
import { LruCache } from "../../cache/LruCache"
|
||||
|
||||
export class ContextCacheProxy implements ICache<string, Context> {
|
||||
private mLruCache: LruCache<string, Context>;
|
||||
private static cache: ContextCacheProxy = new ContextCacheProxy(new LruCache(5));
|
||||
|
||||
private constructor(lruCache: LruCache<string, Context>) {
|
||||
this.mLruCache = lruCache;
|
||||
}
|
||||
|
||||
public static getInstance(): ContextCacheProxy {
|
||||
return ContextCacheProxy.cache;
|
||||
}
|
||||
|
||||
|
||||
// 缓存类型
|
||||
getName() {
|
||||
return "ContextCacheProxy"
|
||||
}
|
||||
|
||||
contextGetValue(key: string, context: Context): Context | undefined{
|
||||
if (this.hasValue(key)) {
|
||||
return this.getValue(key);
|
||||
} else {
|
||||
let moduleContext = context.createModuleContext(key);
|
||||
this.putValue(key, moduleContext)
|
||||
return moduleContext;
|
||||
}
|
||||
}
|
||||
|
||||
getValue(key: string): Context | undefined {
|
||||
return this.mLruCache.get(key);
|
||||
}
|
||||
|
||||
hasValue(key: string): boolean {
|
||||
return this.mLruCache.has(key);
|
||||
}
|
||||
|
||||
putValue(key: string, value: Context) {
|
||||
this.mLruCache.put(key, value);
|
||||
}
|
||||
|
||||
removeValue(key: string): Context | undefined {
|
||||
return this.mLruCache.remove(key);
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.mLruCache.evicAll();
|
||||
}
|
||||
|
||||
|
||||
// 外界调用
|
||||
loadMemoryCache(key: string, isMemoryCacheable: boolean): Context | null {
|
||||
// 是否开启内存缓存
|
||||
if (!isMemoryCacheable) {
|
||||
return null;
|
||||
}
|
||||
let cached = this.getValue(key)
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -19,12 +19,17 @@ import resourceManager from '@ohos.resourceManager';
|
|||
import { ImageKnifeGlobal } from '../ImageKnifeGlobal';
|
||||
import { BusinessError } from '@ohos.base'
|
||||
import common from '@ohos.app.ability.common';
|
||||
import { ContextCacheProxy } from '../requestmanage/ContextCacheProxy';
|
||||
|
||||
export class ParseResClient implements IResourceFetch<ArrayBuffer> {
|
||||
loadResource(context: common.UIAbilityContext, res: Resource, onCompleteFunction: (value: ArrayBuffer) => void | PromiseLike<ArrayBuffer>, onErrorFunction: (reason?: BusinessError | string) => void) {
|
||||
let resId = res.id;
|
||||
let resType = res.type;
|
||||
if (resType == ResourceTypeEts.MEDIA) {
|
||||
(context.createModuleContext(res.moduleName).resourceManager as resourceManager.ResourceManager)
|
||||
let moduleContext = ContextCacheProxy.getInstance()
|
||||
.contextGetValue(res.moduleName, context);
|
||||
if (moduleContext != undefined) {
|
||||
(moduleContext.resourceManager as resourceManager.ResourceManager)
|
||||
.getMediaContent(resId)
|
||||
.then(data => {
|
||||
let arrayBuffer = this.typedArrayToBuffer(data);
|
||||
|
@ -34,6 +39,8 @@ export class ParseResClient implements IResourceFetch<ArrayBuffer> {
|
|||
onErrorFunction(err)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
else if (resType == ResourceTypeEts.RAWFILE) {
|
||||
onErrorFunction('ParseResClient 本地资源是rawfile暂时无法解析出错')
|
||||
} else {
|
||||
|
|
|
@ -20,13 +20,16 @@ import { BusinessError } from '@ohos.base'
|
|||
import resourceManager from '@ohos.resourceManager';
|
||||
import { ImageKnifeGlobal } from '../ImageKnifeGlobal';
|
||||
import common from '@ohos.app.ability.common'
|
||||
import { ContextCacheProxy } from '../requestmanage/ContextCacheProxy'
|
||||
|
||||
export class ParseResClientBase64 implements IResourceFetch<ArrayBuffer> {
|
||||
loadResource(context: common.UIAbilityContext, res: Resource, onCompleteFunction: (value: ArrayBuffer) => void | PromiseLike<ArrayBuffer>, onErrorFunction: (reason?: BusinessError | string) => void) {
|
||||
let resId = res.id;
|
||||
let resType = res.type;
|
||||
if (resType == ResourceTypeEts.MEDIA) {
|
||||
(context.createModuleContext(res.moduleName).resourceManager as resourceManager.ResourceManager)
|
||||
let moduleContext = ContextCacheProxy.getInstance()
|
||||
.contextGetValue(res.moduleName, context);
|
||||
(moduleContext.resourceManager as resourceManager.ResourceManager)
|
||||
.getMediaContentBase64(resId)
|
||||
.then(data => {
|
||||
let matchReg = ';base64,';
|
||||
|
|
|
@ -26,6 +26,7 @@ import resourceManager from '@ohos.resourceManager'
|
|||
import { BusinessError } from '@ohos.base'
|
||||
import {Size} from '../../imageknife/RequestOption'
|
||||
import common from '@ohos.app.ability.common'
|
||||
import { ContextCacheProxy } from '../requestmanage/ContextCacheProxy'
|
||||
|
||||
export class MaskTransformation implements BaseTransform<PixelMap> {
|
||||
private _mResourceData:Resource|undefined = undefined;
|
||||
|
@ -96,7 +97,10 @@ export class MaskTransformation implements BaseTransform<PixelMap> {
|
|||
let context = (request.getModuleContext() as common.UIAbilityContext)
|
||||
if(context != undefined){
|
||||
if(this._mResourceData != undefined){
|
||||
let resourceManager = context.createModuleContext(this._mResourceData.moduleName).resourceManager as resourceManager.ResourceManager
|
||||
let moduleContext = ContextCacheProxy.getInstance()
|
||||
.contextGetValue(this._mResourceData.moduleName, context);
|
||||
if (moduleContext != undefined) {
|
||||
let resourceManager = moduleContext.resourceManager as resourceManager.ResourceManager
|
||||
if(resourceManager != undefined && this._mResourceData != undefined)
|
||||
resourceManager.getMediaContent(this._mResourceData?.id)
|
||||
.then(array => {
|
||||
|
@ -118,6 +122,8 @@ export class MaskTransformation implements BaseTransform<PixelMap> {
|
|||
func?.asyncTransform("MaskTransformation openInternal error" + err, null);
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue