磁盘存储改成同步以及部分release释放放在异步,requestInstream的回调改成异步。

Signed-off-by: zenggaofeng <zenggaofeng2@h-partners.com>
This commit is contained in:
zenggaofeng 2023-12-07 15:07:57 +08:00
parent 1a0947d722
commit 2370557576
7 changed files with 41 additions and 58 deletions

View File

@ -2,7 +2,10 @@
- .jpg .png .gif解码功能使用taskpool实现
- 修复了内存缓存张数设置为1时gif图片消失的问题
- 新增内存缓存策略,新增缓存张数,缓存大小设置接口
- 磁盘存缓存setAsync改成同步
- 部分release释放放在异步
- requestInStream的回调改成异步
- 修复tasktool出现crash问题
## 2.1.1-rc.4

View File

@ -4,7 +4,7 @@
"name": "entry",
"description": "example description",
"repository": {},
"version": "2.1.1-rc.4",
"version": "2.1.1-rc.5",
"dependencies": {
"@ohos/libraryimageknife": "file:../library",
"@ohos/disklrucache": "^2.0.2-rc.0",

View File

@ -138,18 +138,24 @@ export class ImageKnifeData {
}
release() {
let promise = new Promise<void>((resolve) => {
resolve()
})
if (this.isPixelMap()) {
if (this.drawPixelMap != undefined && this.drawPixelMap.imagePixelMap != undefined) {
this.drawPixelMap.isLruCacheRelease = true;
if (this.drawPixelMap.isShowOnComponent){
if (this.drawPixelMap.isShowOnComponent) {
return;
}else {
this.drawPixelMap.imagePixelMap.release()
.then(() => {
if (this.drawPixelMap != undefined && this.drawPixelMap.imagePixelMap != undefined) {
this.drawPixelMap.imagePixelMap = undefined;
}
})
} else {
promise.then(() => {
if (this.drawPixelMap != undefined && this.drawPixelMap.imagePixelMap != undefined) {
this.drawPixelMap.imagePixelMap.release(() => {
if (this.drawPixelMap != undefined && this.drawPixelMap.imagePixelMap != undefined) {
this.drawPixelMap.imagePixelMap = undefined
}
})
}
})
}
LogUtil.info("MemoryLruCache removeMemorySize---- end 释放普通图片:")
}
@ -166,7 +172,11 @@ export class ImageKnifeData {
for (let i = 0; i < gifFrames.length; i++) {
let tempFrame = gifFrames[i];
if (tempFrame.drawPixelMap != undefined) {
tempFrame.drawPixelMap.release()
promise.then(() => {
if (tempFrame.drawPixelMap != undefined) {
tempFrame.drawPixelMap.release()
}
})
}
}
LogUtil.info("MemoryLruCache removeMemorySize---- end 释放GIF图片")

View File

@ -70,15 +70,14 @@ export class HttpDownloadClient implements IDataFetch {
connectTimeout: 60000, // 可选 默认60000ms
readTimeout: 0, //可选, 默认为60000ms
usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定
},
(err: BusinessError, data: number) => {
if (!err && data == 200) {
} else {
onError(`HttpDownloadClient has error, http code = ${data}`)
}
}
)
).then((data)=>{
if(data == 200) {
} else {
onError(`HttpDownloadClient has error, http code = ${data}`)
}
})
} catch (err) {
onError('HttpDownloadClient catch err request uuid ='+request.uuid)
}

View File

@ -41,8 +41,8 @@ export class DiskCacheProxy implements ICache<string, ArrayBuffer> {
return this.mDiskLruCache.get(key);
}
async putValue(key: string, value: ArrayBuffer) {
await this.mDiskLruCache.setAsync(key, value)
putValue(key: string, value: ArrayBuffer) {
this.mDiskLruCache.set(key, value)
}
removeValue(key: string): ArrayBuffer{

View File

@ -466,8 +466,8 @@ export class RequestManager {
// 保存二级磁盘缓存
Promise.resolve(source)
.then(async (arraybuffer: ArrayBuffer)=>{
await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer)
.then((arraybuffer: ArrayBuffer)=>{
this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer)
})
.catch( (err:BusinessError)=>{
LogUtil.log('download file is ='+ImageKnifeData.GIF+'and save diskLruCache error ='+ (err as BusinessError))
@ -480,8 +480,8 @@ export class RequestManager {
// 保存二级磁盘缓存
Promise.resolve(source)
.then(async (arraybuffer: ArrayBuffer)=>{
await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer)
.then((arraybuffer: ArrayBuffer)=>{
this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer)
})
.catch((err:BusinessError)=>{
LogUtil.log('download file is ='+ImageKnifeData.SVG+'and save diskLruCache error ='+ (err as BusinessError))
@ -548,8 +548,8 @@ export class RequestManager {
private saveCacheAndDisk(value: PixelMap, filetype:string, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, source:ArrayBuffer) {
let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value);
this.mMemoryCacheProxy.putValue(this.options.generateCacheKey, imageKnifeData);
let save2DiskCache = async (arraybuffer:ArrayBuffer) => {
await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer)
let save2DiskCache = (arraybuffer:ArrayBuffer) => {
this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer)
// 落盘之后需要主动移除当前request并且调用下一个加载
let removeCurrentAndSearchNextRun = this.options.removeCurrentAndSearchNext
removeCurrentAndSearchNextRun();

View File

@ -26,37 +26,7 @@ export class ParseImageUtil implements IParseImage<PixelMap> {
// scale(0,1)
parseImageThumbnail(scale: number, imageinfo: ArrayBuffer, onCompleteFunction: (value: PixelMap) => void | PromiseLike<PixelMap>, onErrorFunction: (reason?: BusinessError | string) => void) {
// taskPoolExecutePixelMap(imageinfo,scale,onCompleteFunction,onErrorFunction);
let imageSource:image.ImageSource = image.createImageSource(imageinfo); // 步骤一文件转为pixelMap 然后变换 给Image组件
imageSource.getImageInfo((err, value) => {
if (err) {
onErrorFunction(err);
return;
}
let hValue = Math.round(value.size.height * scale);
let wValue = Math.round(value.size.width * scale);
let defaultSize:image.Size = {
height: hValue,
width: wValue
};
let opts:image.DecodingOptions = {
editable: true,
desiredSize: defaultSize
};
imageSource.createPixelMap(opts, (err, pixelmap) => {
if (err) {
onErrorFunction(err);
} else {
onCompleteFunction(pixelmap);
}
imageSource.release()
})
})
taskPoolExecutePixelMap(imageinfo,scale,onCompleteFunction,onErrorFunction);
}
}
@ -86,6 +56,7 @@ async function taskParseImage(arrayBuffer: ArrayBuffer,scale: number): Promise<P
function taskPoolExecutePixelMap(arrayBuffer: ArrayBuffer, scale: number, onCompleteFunction: (value: PixelMap) => void | PromiseLike<PixelMap>, onErrorFunction: (reason?: BusinessError | string) => void) {
LogUtil.log("ceshi321 : arrayBuffer长度" + arrayBuffer.byteLength)
let task = new taskpool.Task(taskParseImage, arrayBuffer,scale)
task.setTransferList([])
taskpool.execute(task).then((pixelmap: image.PixelMap) => {
LogUtil.log('ceshi321 : Succeeded in creating pixelmap Ui .' + pixelmap.getPixelBytesNumber())
onCompleteFunction(pixelmap);