svg解码修改,预加载接口修复,文件缓存目录配置

Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
zgf 2024-06-14 16:41:25 +08:00
parent 5d73ee0b11
commit 150b75fe8c
7 changed files with 36 additions and 21 deletions

View File

@ -1,3 +1,8 @@
## 3.0.0-rc.8
- svg解码单位改为px
- 修复预加载接口preLoadCache传ImageKnifeOption失效
- 文件缓存初始化接口新增目录参数
## 3.0.0-rc.7
- 修复成功回调获取不到宽高
- 新增svg图片解码

View File

@ -14,7 +14,7 @@
"main": "index.ets",
"repository": "https://gitee.com/openharmony-tpc/ImageKnife",
"type": "module",
"version": "3.0.0-rc.7",
"version": "3.0.0-rc.8",
"dependencies": {
"@ohos/gpu_transform": "^1.0.2"
},

View File

@ -65,9 +65,13 @@ export class ImageKnife {
* @param size 缓存数量
* @param memory 内存大小
*/
async initFileCache(context: Context, size: number = 256, memory: number = 256 * 1024 * 1024) {
async initFileCache(context: Context, size: number = 256, memory: number = 256 * 1024 * 1024,path?: string) {
this.fileCache = new FileCache(context, size, memory)
await this.fileCache.initFileCache()
if ( path != undefined ) {
await this.fileCache.initFileCache(path)
} else {
await this.fileCache.initFileCache()
}
}
/**
@ -166,10 +170,10 @@ export class ImageKnife {
preLoadCache(loadSrc: string | ImageKnifeOption): Promise<string> {
return new Promise((resolve, reject) => {
let imageKnifeOption = new ImageKnifeOption()
if (loadSrc instanceof ImageKnifeOption) {
imageKnifeOption = loadSrc
if (typeof loadSrc == "string") {
imageKnifeOption.loadSrc = loadSrc
} else {
imageKnifeOption.loadSrc = loadSrc;
imageKnifeOption = loadSrc;
}
LogUtil.log("ImageKnife_DataTime_preLoadCache-imageKnifeOption:"+loadSrc)
let fileKey = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature)

View File

@ -171,7 +171,8 @@ export class ImageKnifeDispatcher {
signature: currentRequest.imageKnifeOption.signature,
requestSource: requestSource,
isWatchProgress: isWatchProgress,
memoryKey: memoryKey
memoryKey: memoryKey,
fileCacheFolder: ImageKnife.getInstance().getFileCache().getCacheFolder()
}
@ -373,7 +374,7 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
// 判断自定义下载
if (request.customGetImage !== undefined && typeof request.src === 'string' && (request.src.indexOf("http://") == 0 || request.src.indexOf("https://") == 0)) {
// 先从文件缓存获取
resBuf = FileCache.getFileCacheByFile(request.context, fileKey)
resBuf = FileCache.getFileCacheByFile(request.context, fileKey , request.fileCacheFolder)
if (resBuf === undefined) {
LogUtil.log("customGetImage customGetImage");
resBuf = await request.customGetImage(request.context, request.src)
@ -381,7 +382,7 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
if (resBuf !== undefined && request.writeCacheStrategy !== CacheStrategy.Memory) {
let copyBuf = buffer.concat([buffer.from(resBuf)]).buffer; // IDE有bug不能直接获取resBuf.byteLength
bufferSize = copyBuf.byteLength
FileCache.saveFileCacheOnlyFile(request.context, fileKey, resBuf)
FileCache.saveFileCacheOnlyFile(request.context, fileKey, resBuf , request.fileCacheFolder)
}
}
}
@ -389,7 +390,7 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
if (typeof request.src === 'string') {
if (request.src.indexOf("http://") == 0 || request.src.indexOf("https://") == 0) { //从网络下载
// 先从文件缓存获取
resBuf = FileCache.getFileCacheByFile(request.context, fileKey)
resBuf = FileCache.getFileCacheByFile(request.context, fileKey , request.fileCacheFolder)
if (resBuf === undefined && request.onlyRetrieveFromCache != true) {
LogUtil.log("ImageKnife_DataTime_requestJob_httpRequest.start:"+request.src)
let httpRequest = http.createHttp();
@ -454,7 +455,7 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
LogUtil.log("ImageKnife_DataTime_requestJob_saveFileCacheOnlyFile.start:"+request.src)
let copyBuf = combineArrayBuffers(arrayBuffers); // IDE有bug不能直接获取resBuf.byteLength
bufferSize = copyBuf.byteLength
FileCache.saveFileCacheOnlyFile(request.context, fileKey, resBuf)
FileCache.saveFileCacheOnlyFile(request.context, fileKey, resBuf , request.fileCacheFolder)
LogUtil.log("ImageKnife_DataTime_requestJob_saveFileCacheOnlyFile.end:"+request.src)
}
}
@ -545,8 +546,8 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
let hValue = Math.round(request.componentHeight);
let wValue = Math.round(request.componentWidth);
let defaultSize: image.Size = {
height: hValue,
width: wValue
height: vp2px(hValue),
width: vp2px(wValue)
};
let opts: image.DecodingOptions = {
editable: true,

View File

@ -82,5 +82,6 @@ export interface RequestJobRequest {
engineKey: IEngineKey
isWatchProgress: boolean
memoryKey: string
fileCacheFolder: string
}

View File

@ -25,7 +25,7 @@ import { SparkMD5 } from '../3rd_party/sparkmd5/spark-md5';
* 子线程直接读写文件
*/
export class FileCache {
static readonly CACHE_FOLDER: string = "ImageKnife" // context cacheDir 的缓存文件目录
static CACHE_FOLDER: string = "ImageKnife" // context cacheDir 的缓存文件目录
maxMemory: number = 0
currentMemory: number = 0
maxSize: number = 0
@ -56,11 +56,11 @@ export class FileCache {
/**
* 遍历缓存文件目录,初始化缓存
*/
public async initFileCache() {
public async initFileCache(path: string = FileCache.CACHE_FOLDER) {
if (this.isInited) {
return
}
FileCache.CACHE_FOLDER = path
this.path = this.context?.cacheDir + FileUtils.SEPARATOR + FileCache.CACHE_FOLDER + FileUtils.SEPARATOR
await FileUtils.getInstance().createFolder(this.path)
// 遍历缓存目录下的文件,按照时间顺序加入缓存
@ -107,6 +107,10 @@ export class FileCache {
return this.isInited
}
public getCacheFolder(): string {
return FileCache.CACHE_FOLDER
}
// 添加缓存键值对,同时写文件
put(key: string, value: ArrayBuffer): void {
if (key == null || value == null) {
@ -245,10 +249,10 @@ export class FileCache {
* @param key
* @param value
*/
static saveFileCacheOnlyFile(context: Context, key: string, value: ArrayBuffer): boolean {
static saveFileCacheOnlyFile(context: Context, key: string, value: ArrayBuffer, folder: string = FileCache.CACHE_FOLDER): boolean {
// 写文件
FileUtils.getInstance()
.writeFileSync(context.cacheDir + FileUtils.SEPARATOR + FileCache.CACHE_FOLDER + FileUtils.SEPARATOR + key, value)
.writeFileSync(context.cacheDir + FileUtils.SEPARATOR + folder + FileUtils.SEPARATOR + key, value)
return true
}
@ -258,10 +262,10 @@ export class FileCache {
* @param key
* @returns
*/
static getFileCacheByFile(context: Context, key: string): ArrayBuffer | undefined {
static getFileCacheByFile(context: Context, key: string, folder: string = FileCache.CACHE_FOLDER): ArrayBuffer | undefined {
// 从文件获取查看是否有缓存
return FileUtils.getInstance()
.readFileSync(context.cacheDir + FileUtils.SEPARATOR + FileCache.CACHE_FOLDER + FileUtils.SEPARATOR + key)
.readFileSync(context.cacheDir + FileUtils.SEPARATOR + folder + FileUtils.SEPARATOR + key)
}
/**

View File

@ -16,6 +16,6 @@ import { ImageKnife } from '@ohos/imageknife'
import common from '@ohos.app.ability.common'
export class InitImageKnife{
static async init(entryContext:common.UIAbilityContext){
await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024)
await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"ImageKnifeCache")
}
}