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

Merge pull request !307 from zgf/3.x
This commit is contained in:
Madi 2024-06-14 09:11:31 +00:00 committed by Gitee
commit d42caf8188
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 44 additions and 22 deletions

View File

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

View File

@ -26,11 +26,18 @@ struct TestPrefetchToFileCachePage {
let fileCachePath = await ImageKnife.getInstance().preLoadCache(url) let fileCachePath = await ImageKnife.getInstance().preLoadCache(url)
console.log("preload-fileCachePath=="+ fileCachePath) console.log("preload-fileCachePath=="+ fileCachePath)
} }
async preload1(url:string) {
let fileCachePath = await ImageKnife.getInstance().preLoadCache({ loadSrc: url })
console.log("preload-fileCachePath1=="+ fileCachePath)
}
build() { build() {
Column() { Column() {
Button("预加载图片到文件缓存").margin({top:10}).onClick(async ()=>{ Button("url预加载图片到文件缓存").margin({top:10}).onClick(async ()=>{
await this.preload("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658") await this.preload("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658")
}) })
Button("option预加载图片到文件缓存").margin({top:10}).onClick(async ()=>{
await this.preload1("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658")
})
Button("加载图片(预加载后可断网加载)").margin({top:10}).onClick(()=>{ Button("加载图片(预加载后可断网加载)").margin({top:10}).onClick(()=>{
this.imageKnifeOption.loadSrc = "https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658" this.imageKnifeOption.loadSrc = "https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658"
}) })

View File

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

View File

@ -65,10 +65,14 @@ export class ImageKnife {
* @param size 缓存数量 * @param size 缓存数量
* @param memory 内存大小 * @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) this.fileCache = new FileCache(context, size, memory)
if ( path != undefined ) {
await this.fileCache.initFileCache(path)
} else {
await this.fileCache.initFileCache() await this.fileCache.initFileCache()
} }
}
/** /**
* 判断文件缓存是否初始化 * 判断文件缓存是否初始化
@ -166,10 +170,10 @@ export class ImageKnife {
preLoadCache(loadSrc: string | ImageKnifeOption): Promise<string> { preLoadCache(loadSrc: string | ImageKnifeOption): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let imageKnifeOption = new ImageKnifeOption() let imageKnifeOption = new ImageKnifeOption()
if (loadSrc instanceof ImageKnifeOption) { if (typeof loadSrc == "string") {
imageKnifeOption = loadSrc imageKnifeOption.loadSrc = loadSrc
} else { } else {
imageKnifeOption.loadSrc = loadSrc; imageKnifeOption = loadSrc;
} }
LogUtil.log("ImageKnife_DataTime_preLoadCache-imageKnifeOption:"+loadSrc) LogUtil.log("ImageKnife_DataTime_preLoadCache-imageKnifeOption:"+loadSrc)
let fileKey = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature) let fileKey = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature)

View File

@ -171,7 +171,8 @@ export class ImageKnifeDispatcher {
signature: currentRequest.imageKnifeOption.signature, signature: currentRequest.imageKnifeOption.signature,
requestSource: requestSource, requestSource: requestSource,
isWatchProgress: isWatchProgress, 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)) { 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) { if (resBuf === undefined) {
LogUtil.log("customGetImage customGetImage"); LogUtil.log("customGetImage customGetImage");
resBuf = await request.customGetImage(request.context, request.src) 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) { if (resBuf !== undefined && request.writeCacheStrategy !== CacheStrategy.Memory) {
let copyBuf = buffer.concat([buffer.from(resBuf)]).buffer; // IDE有bug不能直接获取resBuf.byteLength let copyBuf = buffer.concat([buffer.from(resBuf)]).buffer; // IDE有bug不能直接获取resBuf.byteLength
bufferSize = copyBuf.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 (typeof request.src === 'string') {
if (request.src.indexOf("http://") == 0 || request.src.indexOf("https://") == 0) { //从网络下载 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) { if (resBuf === undefined && request.onlyRetrieveFromCache != true) {
LogUtil.log("ImageKnife_DataTime_requestJob_httpRequest.start:"+request.src) LogUtil.log("ImageKnife_DataTime_requestJob_httpRequest.start:"+request.src)
let httpRequest = http.createHttp(); 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) LogUtil.log("ImageKnife_DataTime_requestJob_saveFileCacheOnlyFile.start:"+request.src)
let copyBuf = combineArrayBuffers(arrayBuffers); // IDE有bug不能直接获取resBuf.byteLength let copyBuf = combineArrayBuffers(arrayBuffers); // IDE有bug不能直接获取resBuf.byteLength
bufferSize = copyBuf.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) 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 hValue = Math.round(request.componentHeight);
let wValue = Math.round(request.componentWidth); let wValue = Math.round(request.componentWidth);
let defaultSize: image.Size = { let defaultSize: image.Size = {
height: hValue, height: vp2px(hValue),
width: wValue width: vp2px(wValue)
}; };
let opts: image.DecodingOptions = { let opts: image.DecodingOptions = {
editable: true, editable: true,

View File

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

View File

@ -25,7 +25,7 @@ import { SparkMD5 } from '../3rd_party/sparkmd5/spark-md5';
* 子线程直接读写文件 * 子线程直接读写文件
*/ */
export class FileCache { export class FileCache {
static readonly CACHE_FOLDER: string = "ImageKnife" // context cacheDir 的缓存文件目录 static CACHE_FOLDER: string = "ImageKnife" // context cacheDir 的缓存文件目录
maxMemory: number = 0 maxMemory: number = 0
currentMemory: number = 0 currentMemory: number = 0
maxSize: 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) { if (this.isInited) {
return return
} }
FileCache.CACHE_FOLDER = path
this.path = this.context?.cacheDir + FileUtils.SEPARATOR + FileCache.CACHE_FOLDER + FileUtils.SEPARATOR this.path = this.context?.cacheDir + FileUtils.SEPARATOR + FileCache.CACHE_FOLDER + FileUtils.SEPARATOR
await FileUtils.getInstance().createFolder(this.path) await FileUtils.getInstance().createFolder(this.path)
// 遍历缓存目录下的文件,按照时间顺序加入缓存 // 遍历缓存目录下的文件,按照时间顺序加入缓存
@ -107,6 +107,10 @@ export class FileCache {
return this.isInited return this.isInited
} }
public getCacheFolder(): string {
return FileCache.CACHE_FOLDER
}
// 添加缓存键值对,同时写文件 // 添加缓存键值对,同时写文件
put(key: string, value: ArrayBuffer): void { put(key: string, value: ArrayBuffer): void {
if (key == null || value == null) { if (key == null || value == null) {
@ -245,10 +249,10 @@ export class FileCache {
* @param key * @param key
* @param value * @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() 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 return true
} }
@ -258,10 +262,10 @@ export class FileCache {
* @param key * @param key
* @returns * @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() 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' import common from '@ohos.app.ability.common'
export class InitImageKnife{ export class InitImageKnife{
static async init(entryContext:common.UIAbilityContext){ 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")
} }
} }