跳过网络,从内存中取图片,md文档
This commit is contained in:
parent
179917f23b
commit
c5ff1753c6
|
@ -55,8 +55,11 @@ struct testImageKnifeCache {
|
|||
errorholderSrc: $r('app.media.icon_failed')
|
||||
};
|
||||
|
||||
hasUrlCache(request: RequestOption) {
|
||||
imageKnife?.isUrlExist(request).then((data: ImageKnifeData) => {
|
||||
hasUrlCache(url: string, cacheType: CacheType = CacheType.Default, size: Size = {
|
||||
width: 0,
|
||||
height: 0
|
||||
}) {
|
||||
imageKnife?.isUrlExist(url, cacheType, size).then((data: ImageKnifeData) => {
|
||||
clearTimeout(this.timeId);
|
||||
if (data.isPixelMap()) {
|
||||
if (data.drawPixelMap) {
|
||||
|
@ -183,31 +186,26 @@ struct testImageKnifeCache {
|
|||
errorholderSrc: $r('app.media.icon_failed')
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
Row() {
|
||||
Button('缓存图片')
|
||||
.onClick(() => {
|
||||
this.index_ = 1;
|
||||
let request = new RequestOption();
|
||||
request.load(this.url)
|
||||
.setImageViewSize(this.comSize)
|
||||
.setCacheType(CacheType.Cache);
|
||||
this.hasUrlCache(request);
|
||||
this.hasUrlCache(this.url, CacheType.Cache, this.comSize);
|
||||
|
||||
})
|
||||
|
||||
Button('磁盘图片')
|
||||
.onClick(() => {
|
||||
this.index_ = 2;
|
||||
let request = new RequestOption();
|
||||
request.load(this.url)
|
||||
.setImageViewSize(this.comSize)
|
||||
.setCacheType(CacheType.Disk);
|
||||
this.hasUrlCache(request);
|
||||
this.hasUrlCache(this.url, CacheType.Disk, this.comSize);
|
||||
})
|
||||
|
||||
Button('默认')
|
||||
.onClick(() => {
|
||||
this.index_ = 2;
|
||||
this.hasUrlCache(this.url);
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -328,25 +328,40 @@ export class ImageKnife {
|
|||
return this.parseSource(request);
|
||||
}
|
||||
|
||||
|
||||
public isUrlExist(request: RequestOption): Promise<ImageKnifeData> {
|
||||
|
||||
public isUrlExist(url: string, cacheType: CacheType = CacheType.Default, size: Size = {
|
||||
width: 0,
|
||||
height: 0
|
||||
}): Promise<ImageKnifeData> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let request = new RequestOption();
|
||||
request.load(url)
|
||||
.setImageViewSize(size)
|
||||
.setCacheType(cacheType);
|
||||
this.generateDataCacheKey(request);
|
||||
let loadComplete = (imageKnifeData: ImageKnifeData) => {
|
||||
resolve(imageKnifeData);
|
||||
}
|
||||
let loadError = (err ?: BusinessError | string) => {
|
||||
if (request.cacheType == CacheType.Default) {
|
||||
this.loadMemoryDiskIsUrl(request, loadComplete, loadError);
|
||||
}
|
||||
reject(err);
|
||||
}
|
||||
this.loadMemoryCacheAndDiskFrom(request, loadComplete, loadError);
|
||||
|
||||
if (request.cacheType == CacheType.Cache) {
|
||||
this.loadMemoryCacheIsUrl(request, loadComplete, loadError);
|
||||
|
||||
} else if (request.cacheType == CacheType.Disk) {
|
||||
this.loadMemoryDiskIsUrl(request, loadComplete, loadError);
|
||||
}
|
||||
else if (request.cacheType == CacheType.Default) {
|
||||
this.loadMemoryCacheIsUrl(request, loadComplete, loadError);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
loadMemoryCacheAndDiskFrom(request: RequestOption, onComplete: (imageKnifeData: ImageKnifeData) => void | PromiseLike<ImageKnifeData>, onError: (err?: BusinessError | string) => void) {
|
||||
|
||||
if (request.cacheType == CacheType.Cache) {
|
||||
|
||||
loadMemoryCacheIsUrl(request: RequestOption, onComplete: (imageKnifeData: ImageKnifeData) => void | PromiseLike<ImageKnifeData>, onError: (err?: BusinessError | string) => void) {
|
||||
let cache = this.memoryCacheProxy.loadMemoryCache(request.generateCacheKey, true);
|
||||
if (cache == null || typeof cache == 'undefined') {
|
||||
onError("No data in cache!")
|
||||
|
@ -355,8 +370,9 @@ export class ImageKnife {
|
|||
//2.网络缓存有数据,返回
|
||||
onComplete(cache);
|
||||
}
|
||||
}
|
||||
|
||||
}else if (request.cacheType == CacheType.Disk){
|
||||
loadMemoryDiskIsUrl(request: RequestOption, onComplete: (imageKnifeData: ImageKnifeData) => void | PromiseLike<ImageKnifeData>, onError: (err?: BusinessError | string) => void) {
|
||||
let cached: ArrayBuffer = DiskLruCache.getFileCacheByFile(this.diskMemoryCache.getPath() as string, request.generateDataKey) as ArrayBuffer;
|
||||
if (cached != null) {
|
||||
let filetype: string | null = this.fileTypeUtil.getFileType(cached);
|
||||
|
@ -406,7 +422,7 @@ export class ImageKnife {
|
|||
//6.磁盘无数据,返回onError
|
||||
onError("No data in disk cache!")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -827,7 +843,8 @@ export class ImageKnife {
|
|||
if (cachedPath == null || cachedPath == "" || cachedPath == undefined) {
|
||||
let request = new RequestOption();
|
||||
request.load(url)
|
||||
.addListener({ callback: (err: BusinessError | string, data: ImageKnifeData) => {
|
||||
.addListener({
|
||||
callback: (err: BusinessError | string, data: ImageKnifeData) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
|
|
|
@ -65,6 +65,7 @@ export interface Size {
|
|||
}
|
||||
|
||||
export enum CacheType {
|
||||
Default,
|
||||
//缓存
|
||||
Cache,
|
||||
//磁盘
|
||||
|
|
Loading…
Reference in New Issue