跳过网络,从内存中取图片
This commit is contained in:
parent
ca53509ebc
commit
d674f6c409
|
@ -41,6 +41,9 @@ import common from '@ohos.app.ability.common'
|
||||||
import HashMap from '@ohos.util.HashMap'
|
import HashMap from '@ohos.util.HashMap'
|
||||||
import LinkedList from '@ohos.util.LinkedList'
|
import LinkedList from '@ohos.util.LinkedList'
|
||||||
import { MemoryLruCache } from '../cache/MemoryLruCache'
|
import { MemoryLruCache } from '../cache/MemoryLruCache'
|
||||||
|
import { BusinessError } from '@ohos.base'
|
||||||
|
import { IParseImage } from './interface/IParseImage'
|
||||||
|
import { ParseImageUtil } from './utils/ParseImageUtil'
|
||||||
|
|
||||||
export class ImageKnife {
|
export class ImageKnife {
|
||||||
static readonly SEPARATOR: string = '/'
|
static readonly SEPARATOR: string = '/'
|
||||||
|
@ -70,9 +73,10 @@ export class ImageKnife {
|
||||||
defaultLifeCycle: IDrawLifeCycle | undefined = undefined;
|
defaultLifeCycle: IDrawLifeCycle | undefined = undefined;
|
||||||
// 开发者可配置全局缓存
|
// 开发者可配置全局缓存
|
||||||
engineKeyImpl: EngineKeyInterface;
|
engineKeyImpl: EngineKeyInterface;
|
||||||
|
private mParseImageUtil: IParseImage<PixelMap>;
|
||||||
|
|
||||||
private constructor() {
|
private constructor() {
|
||||||
|
this.mParseImageUtil = new ParseImageUtil();
|
||||||
this.runningMaps = new EasyLinkedHashMap();
|
this.runningMaps = new EasyLinkedHashMap();
|
||||||
this.pendingMaps = new EasyLinkedHashMap();
|
this.pendingMaps = new EasyLinkedHashMap();
|
||||||
this.pausedMaps = new EasyLinkedHashMap();
|
this.pausedMaps = new EasyLinkedHashMap();
|
||||||
|
@ -99,6 +103,7 @@ export class ImageKnife {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//全局设置请求头调用方法
|
//全局设置请求头调用方法
|
||||||
addHeader(key: string, value: Object) {
|
addHeader(key: string, value: Object) {
|
||||||
this.headerMap.set(key, value);
|
this.headerMap.set(key, value);
|
||||||
|
@ -214,6 +219,106 @@ export class ImageKnife {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public isUrlExist(url: string): Promise<PixelMap> {
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let request1 = new RequestOption();
|
||||||
|
request1.loadSrc = url;
|
||||||
|
let request = this.loadResourcesCaches(request1);
|
||||||
|
|
||||||
|
let loadComplete = (ImageKnifeData: ImageKnifeData) => {
|
||||||
|
let pixelMap: PixelMap | undefined;
|
||||||
|
pixelMap = ImageKnifeData?.drawPixelMap?.imagePixelMap;
|
||||||
|
resolve(pixelMap as PixelMap);
|
||||||
|
}
|
||||||
|
let loadError = (err ?: BusinessError | string) => {
|
||||||
|
reject(undefined);
|
||||||
|
}
|
||||||
|
this.loadMemoryCacheAndDiskFrom(request, loadComplete, loadError);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
loadMemoryCacheAndDiskFrom(request: RequestOption, onComplate: (ImageKnifeData: ImageKnifeData) => void | PromiseLike<ImageKnifeData>, onError: (err?: BusinessError | string) => void) {
|
||||||
|
let cache = this.memoryCache.get(request.generateCacheKey);
|
||||||
|
if (cache == null || typeof cache == 'undefined') {
|
||||||
|
let cached = this.diskMemoryCache.get(request.generateDataKey);
|
||||||
|
if (cached == null) {
|
||||||
|
//4.加载磁盘缓存 原图
|
||||||
|
cached = this.diskMemoryCache.get(request.generateResourceKey);
|
||||||
|
}
|
||||||
|
if (cached != null) {
|
||||||
|
//5.磁盘有数据,解析错误返回onError
|
||||||
|
let success = (value: PixelMap) => {
|
||||||
|
let imageKnifeData = ImageKnifeData.createImagePixelMap(ImageKnifeType.PIXELMAP, value);
|
||||||
|
this.memoryCache.put(request.generateCacheKey, imageKnifeData);
|
||||||
|
onComplate(imageKnifeData);
|
||||||
|
}
|
||||||
|
this.mParseImageUtil.parseImage(cached, success, onError)
|
||||||
|
} else {
|
||||||
|
//6.磁盘无数据,返回onError
|
||||||
|
onError("网络缓存and磁盘缓存无数据!")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//需要清理状态
|
||||||
|
cache.waitSaveDisk = false;
|
||||||
|
//2.网络缓存有数据,返回
|
||||||
|
onComplate(cache);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadResourcesCaches(request: RequestOption) {
|
||||||
|
let factories: EngineKeyInterface;
|
||||||
|
let cacheKey: string;
|
||||||
|
let transferKey: string;
|
||||||
|
let dataKey: string;
|
||||||
|
if (this.engineKeyImpl) {
|
||||||
|
factories = this.engineKeyImpl;
|
||||||
|
} else {
|
||||||
|
factories = new EngineKeyFactories();
|
||||||
|
}
|
||||||
|
// 生成内存缓存key 内存 变换后磁盘
|
||||||
|
|
||||||
|
let loadKey = '';
|
||||||
|
if (typeof request.loadSrc == 'string') {
|
||||||
|
loadKey = request.loadSrc;
|
||||||
|
} else {
|
||||||
|
loadKey = JSON.stringify(request.loadSrc);
|
||||||
|
}
|
||||||
|
|
||||||
|
let size = JSON.stringify(request.size);
|
||||||
|
|
||||||
|
let transformed = '';
|
||||||
|
if (request && request.transformations) {
|
||||||
|
for (let i = 0; i < request.transformations.length; i++) {
|
||||||
|
if (i == request.transformations.length - 1) {
|
||||||
|
transformed += request.transformations[i].getName() + "";
|
||||||
|
} else {
|
||||||
|
transformed += request.transformations[i].getName() + ",";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let dontAnimateFlag = request.dontAnimateFlag;
|
||||||
|
|
||||||
|
let signature = request.signature;
|
||||||
|
|
||||||
|
cacheKey = factories.generateMemoryCacheKey(loadKey, size, transformed, dontAnimateFlag, signature);
|
||||||
|
|
||||||
|
// 生成磁盘缓存变换后数据key 变换后数据保存在磁盘
|
||||||
|
transferKey = factories.generateTransformedDiskCacheKey(loadKey, size, transformed, dontAnimateFlag, signature);
|
||||||
|
|
||||||
|
// 生成磁盘缓存源数据key 原始数据保存在磁盘
|
||||||
|
dataKey = factories.generateOriginalDiskCacheKey(loadKey, signature);
|
||||||
|
|
||||||
|
request.generateCacheKey = cacheKey;
|
||||||
|
request.generateResourceKey = transferKey;
|
||||||
|
request.generateDataKey = dataKey;
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 预加载 resource资源一级缓存,string资源实现二级缓存
|
// 预加载 resource资源一级缓存,string资源实现二级缓存
|
||||||
preload(request: RequestOption): void {
|
preload(request: RequestOption): void {
|
||||||
// 每个request 公共信息补充
|
// 每个request 公共信息补充
|
||||||
|
@ -308,6 +413,7 @@ export class ImageKnife {
|
||||||
return this.parseSource(request);
|
return this.parseSource(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
loadResources(request: RequestOption) {
|
loadResources(request: RequestOption) {
|
||||||
let factories: EngineKeyInterface;
|
let factories: EngineKeyInterface;
|
||||||
let cacheKey: string;
|
let cacheKey: string;
|
||||||
|
@ -375,8 +481,6 @@ export class ImageKnife {
|
||||||
private keyEqualPendingToRun(nextPending: RequestOption) {
|
private keyEqualPendingToRun(nextPending: RequestOption) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
this.pendingMaps.remove(nextPending.uuid)
|
this.pendingMaps.remove(nextPending.uuid)
|
||||||
this.runningMaps.put(nextPending.uuid, nextPending);
|
this.runningMaps.put(nextPending.uuid, nextPending);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue