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