!17 1.delete native disklrucahe & changed all media pictures
Merge pull request !17 from 周黎生/master
7
OAT.xml
|
@ -16,6 +16,13 @@
|
||||||
</filefilter>
|
</filefilter>
|
||||||
<filefilter name="binaryFileTypePolicyFilter" desc="Filters for binary file policies">
|
<filefilter name="binaryFileTypePolicyFilter" desc="Filters for binary file policies">
|
||||||
<filteritem type="filename" name="*.dpg" desc="dpg图片格式文件,用于展示示例"/>
|
<filteritem type="filename" name="*.dpg" desc="dpg图片格式文件,用于展示示例"/>
|
||||||
|
<filteritem type="filename" name="*.png" desc="png图片格式文件,用于展示示例"/>
|
||||||
|
<filteritem type="filename" name="*.bmp" desc="bmp图片格式文件,用于展示示例"/>
|
||||||
|
<filteritem type="filename" name="*.webp" desc="webp图片格式文件,用于展示示例"/>
|
||||||
|
<filteritem type="filename" name="*.svg" desc="svg图片格式文件,用于展示示例"/>
|
||||||
|
<filteritem type="filename" name="*.gif" desc="gif图片格式文件,用于展示示例"/>
|
||||||
|
<filteritem type="filename" name="*.jpg" desc="jpg图片格式文件,用于展示示例"/>
|
||||||
|
<filteritem type="filename" name="*.jpeg" desc="jpeg图片格式文件,用于展示示例"/>
|
||||||
</filefilter>
|
</filefilter>
|
||||||
</filefilterlist>
|
</filefilterlist>
|
||||||
</oatconfig>
|
</oatconfig>
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
"products": [
|
"products": [
|
||||||
{
|
{
|
||||||
"name": "default",
|
"name": "default",
|
||||||
"signingConfig": "default",
|
"signingConfig": "default"
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
},
|
},
|
||||||
"modules": [
|
"modules": [
|
||||||
{
|
{
|
||||||
|
@ -25,10 +25,6 @@
|
||||||
{
|
{
|
||||||
"name": "imageknife",
|
"name": "imageknife",
|
||||||
"srcPath": "./imageknife"
|
"srcPath": "./imageknife"
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "disklrucache",
|
|
||||||
"srcPath": "./disklrucache"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -1,3 +0,0 @@
|
||||||
/node_modules
|
|
||||||
/.preview
|
|
||||||
/build
|
|
|
@ -1,5 +0,0 @@
|
||||||
{
|
|
||||||
"apiType": "stageMode",
|
|
||||||
"buildOption": {
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,3 +0,0 @@
|
||||||
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
|
|
||||||
module.exports = require('@ohos/hvigor-ohos-plugin').harTasks
|
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the 'License');
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http:// www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an 'AS IS' BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* disklrucache
|
|
||||||
*/
|
|
||||||
export * from './src/main/ets/components/disklrucache/DiskLruCache'
|
|
||||||
export * from './src/main/ets/components/disklrucache/DiskCacheEntry'
|
|
|
@ -1,18 +0,0 @@
|
||||||
{
|
|
||||||
"license":"ISC",
|
|
||||||
"types":"",
|
|
||||||
"devDependencies":{
|
|
||||||
"@types/spark-md5":"^3.0.2"
|
|
||||||
},
|
|
||||||
"name":"@ohos/disklrucache",
|
|
||||||
"description":"a npm package which contains arkUI2.0 page",
|
|
||||||
"ohos":{
|
|
||||||
"org":""
|
|
||||||
},
|
|
||||||
"main":"index.ets",
|
|
||||||
"repository":{},
|
|
||||||
"version":"1.0.0",
|
|
||||||
"dependencies":{
|
|
||||||
"spark-md5":"^3.0.2"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,115 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the 'License');
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http:// www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an 'AS IS' BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
export class CustomMap <K, V> {
|
|
||||||
map: Map<K, V> = new Map<K, V>()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取键对应的值
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
*/
|
|
||||||
get(key: K): V | undefined {
|
|
||||||
if (key == null) {
|
|
||||||
throw new Error('key is null,checking the parameter');
|
|
||||||
}
|
|
||||||
return this.map.get(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否含有key的缓存
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
*/
|
|
||||||
hasKey(key: K) {
|
|
||||||
if (key == null) {
|
|
||||||
throw new Error('key is null,checking the parameter');
|
|
||||||
}
|
|
||||||
return this.map.has(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 添加键值对
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
* @param value 键对应的值
|
|
||||||
*/
|
|
||||||
put(key: K, value: V): V | undefined {
|
|
||||||
if (key == null || value == null) {
|
|
||||||
throw new Error('key or value is invalid,checking the parameter');
|
|
||||||
}
|
|
||||||
let pre = this.map.get(key)
|
|
||||||
if (this.hasKey(key)) {
|
|
||||||
this.map.delete(key)
|
|
||||||
}
|
|
||||||
this.map.set(key, value);
|
|
||||||
return pre
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 去除键值,(去除键数据中的键名及对应的值)
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
*/
|
|
||||||
remove(key: K): boolean {
|
|
||||||
if (key == null) {
|
|
||||||
throw new Error('key is null,checking the parameter');
|
|
||||||
}
|
|
||||||
return this.map.delete(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取最先存储的数据的key
|
|
||||||
*/
|
|
||||||
getFirstKey(): K { // keys()可以遍历后需要优化put()方法,暂时仅获取index=0的key
|
|
||||||
return this.map.keys().next().value
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断键值元素是否为空
|
|
||||||
*/
|
|
||||||
isEmpty(): boolean {
|
|
||||||
return this.map.size == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取键值元素大小
|
|
||||||
*/
|
|
||||||
size(): number {
|
|
||||||
return this.map.size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 遍历Map,执行处理函数. 回调函数 function(key,value,index){..}
|
|
||||||
*
|
|
||||||
* @param fn 遍历回调方法
|
|
||||||
*/
|
|
||||||
each(fn) {
|
|
||||||
this.map.forEach(fn)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清除键值对
|
|
||||||
*/
|
|
||||||
clear() {
|
|
||||||
this.map.clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 遍历key
|
|
||||||
*/
|
|
||||||
keys(): IterableIterator<K> {
|
|
||||||
return this.map.keys()
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,46 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
export class DiskCacheEntry {
|
|
||||||
// 缓存的key
|
|
||||||
key: string = ''
|
|
||||||
|
|
||||||
// 缓存文件大小
|
|
||||||
length: number = 0
|
|
||||||
|
|
||||||
constructor(key: string, length?: number) {
|
|
||||||
this.key = key
|
|
||||||
this.length = length
|
|
||||||
}
|
|
||||||
|
|
||||||
setKey(key: string) {
|
|
||||||
this.key = key
|
|
||||||
}
|
|
||||||
|
|
||||||
getKey(): string {
|
|
||||||
return this.key
|
|
||||||
}
|
|
||||||
|
|
||||||
setLength(length: number) {
|
|
||||||
this.length = length
|
|
||||||
}
|
|
||||||
|
|
||||||
getLength(): number {
|
|
||||||
return this.length
|
|
||||||
}
|
|
||||||
|
|
||||||
toString(): string {
|
|
||||||
return this.key + ' - ' + this.length
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,444 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the 'License');
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an 'AS IS' BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
import fileio from '@ohos.fileio'
|
|
||||||
import { CustomMap } from './CustomMap'
|
|
||||||
import { FileUtils } from './FileUtils'
|
|
||||||
import { FileReader } from './FileReader'
|
|
||||||
import { DiskCacheEntry } from './DiskCacheEntry'
|
|
||||||
import SparkMD5 from "spark-md5"
|
|
||||||
|
|
||||||
|
|
||||||
export class DiskLruCache {
|
|
||||||
// 默认缓存数据最大值
|
|
||||||
private static readonly DEFAULT_MAX_SIZE: number = 30 * 1024 * 1024
|
|
||||||
|
|
||||||
// 缓存journal文件名称
|
|
||||||
private static readonly journal: string = 'journal'
|
|
||||||
|
|
||||||
// 缓存journal备份文件名称
|
|
||||||
private static readonly journalTemp: string = 'journal_temp'
|
|
||||||
|
|
||||||
// 备份文件save标识符
|
|
||||||
private static readonly SAVE: string = 'save'
|
|
||||||
|
|
||||||
// 备份文件read标识符
|
|
||||||
private static readonly READ: string = 'read'
|
|
||||||
|
|
||||||
// 备份文件remove标识符
|
|
||||||
private static readonly REMOVE: string = 'remove'
|
|
||||||
|
|
||||||
// 缓存文件路径地址
|
|
||||||
private path: string = ''
|
|
||||||
|
|
||||||
// 缓存journal文件路径
|
|
||||||
private journalPath: string = ''
|
|
||||||
|
|
||||||
// 缓存journal备份文件路径
|
|
||||||
private journalPathTemp: string = ''
|
|
||||||
|
|
||||||
// 缓存数据最大值
|
|
||||||
private maxSize: number = DiskLruCache.DEFAULT_MAX_SIZE
|
|
||||||
|
|
||||||
// 当前缓存数据值
|
|
||||||
private size: number = 0
|
|
||||||
|
|
||||||
// 缓存数据集合
|
|
||||||
private cacheMap: CustomMap<string, DiskCacheEntry> = new CustomMap<string, DiskCacheEntry>()
|
|
||||||
|
|
||||||
private constructor(path: string, maxSize: number) {
|
|
||||||
this.path = path
|
|
||||||
this.maxSize = maxSize
|
|
||||||
this.journalPath = path + DiskLruCache.journal
|
|
||||||
this.journalPathTemp = path + DiskLruCache.journalTemp
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 打开path中的缓存,如果不存在缓存,则创建新缓存
|
|
||||||
*
|
|
||||||
* @param path 缓存文件路径地址
|
|
||||||
* @param maxSize 缓存数据最大值,默认值为3M
|
|
||||||
*/
|
|
||||||
public static create(path: string, maxSize?: number): DiskLruCache {
|
|
||||||
if (!!!path) {
|
|
||||||
throw new Error('DiskLruCache create path is empty, checking the parameter');
|
|
||||||
}
|
|
||||||
if (!!!maxSize) {
|
|
||||||
maxSize = DiskLruCache.DEFAULT_MAX_SIZE
|
|
||||||
}
|
|
||||||
if (maxSize <= 0) {
|
|
||||||
throw new Error("DiskLruCache create maxSize <= 0, checking the parameter");
|
|
||||||
}
|
|
||||||
if (!FileUtils.getInstance().existFolder(path)) {
|
|
||||||
FileUtils.getInstance().createFolder(path, true)
|
|
||||||
}
|
|
||||||
if (path.endsWith(FileUtils.SEPARATOR)) {
|
|
||||||
path = path
|
|
||||||
} else {
|
|
||||||
path = path + FileUtils.SEPARATOR
|
|
||||||
}
|
|
||||||
let journalPath = path + DiskLruCache.journal
|
|
||||||
let journalPathTemp = path + DiskLruCache.journalTemp
|
|
||||||
|
|
||||||
// 判断日志文件是否存在,如果没有初始化创建
|
|
||||||
if (FileUtils.getInstance().exist(journalPath)) {
|
|
||||||
let stat = fileio.statSync(journalPath)
|
|
||||||
if (stat.size > 0) {
|
|
||||||
FileUtils.getInstance().createFile(journalPathTemp)
|
|
||||||
FileUtils.getInstance().copyFile(journalPath, journalPathTemp)
|
|
||||||
let diskLruCache: DiskLruCache = new DiskLruCache(path, maxSize)
|
|
||||||
diskLruCache.readJournal(journalPathTemp)
|
|
||||||
diskLruCache.resetJournalFile()
|
|
||||||
return diskLruCache
|
|
||||||
} else {
|
|
||||||
return new DiskLruCache(path, maxSize)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
FileUtils.getInstance().createFile(journalPath)
|
|
||||||
return new DiskLruCache(path, maxSize)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理journal文件数据
|
|
||||||
*
|
|
||||||
* @param line 日志行字符串
|
|
||||||
*/
|
|
||||||
private dealWithJournal(line: string) {
|
|
||||||
let filePath = ''
|
|
||||||
try {
|
|
||||||
let lineData = line.split(' ')
|
|
||||||
if (lineData.length > 1) {
|
|
||||||
if (lineData[0] != DiskLruCache.REMOVE) {
|
|
||||||
filePath = this.path + lineData[1]
|
|
||||||
let fileStat = fileio.statSync(filePath)
|
|
||||||
if (fileStat.isFile() && fileStat.size > 0) {
|
|
||||||
this.size = this.size + fileStat.size
|
|
||||||
FileUtils.getInstance().writeData(this.journalPath, line + FileReader.LF)
|
|
||||||
this.putCacheMap(lineData[1], fileStat.size)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (this.cacheMap.hasKey(lineData[1])) {
|
|
||||||
let cacheEntry: DiskCacheEntry = this.cacheMap.get(lineData[1])
|
|
||||||
this.size = this.size - cacheEntry.getLength()
|
|
||||||
this.cacheMap.remove(lineData[1])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error('DiskLruCache - dealWithJournal e ' + e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置disk缓存最大数据值
|
|
||||||
*
|
|
||||||
* @param max 缓存数据最大值
|
|
||||||
*/
|
|
||||||
setMaxSize(max: number) {
|
|
||||||
this.maxSize = max
|
|
||||||
this.trimToSize()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 存储disk缓存数据
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
* @param content 文件内容
|
|
||||||
*/
|
|
||||||
set(key: string, content: ArrayBuffer) {
|
|
||||||
if (!!!key) {
|
|
||||||
throw new Error('key is null, checking the parameter')
|
|
||||||
}
|
|
||||||
if (content == null || content.byteLength == 0) {
|
|
||||||
throw new Error('content is null, checking the parameter')
|
|
||||||
}
|
|
||||||
let fileSize = content.byteLength
|
|
||||||
key = SparkMD5.hash(key)
|
|
||||||
this.size = this.size + fileSize
|
|
||||||
this.putCacheMap(key, fileSize)
|
|
||||||
FileUtils.getInstance().writeData(this.journalPath, DiskLruCache.SAVE + ' ' + key + FileReader.LF)
|
|
||||||
this.trimToSize()
|
|
||||||
let tempPath = this.path + key
|
|
||||||
FileUtils.getInstance().writeNewFile(tempPath, content)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步存储disk缓存数据
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
* @param content 文件内容
|
|
||||||
*/
|
|
||||||
async setAsync(key: string, content: ArrayBuffer): Promise<void> {
|
|
||||||
if (!!!key) {
|
|
||||||
throw new Error('key is null, checking the parameter')
|
|
||||||
}
|
|
||||||
if (content == null || content.byteLength == 0) {
|
|
||||||
throw new Error('content is null, checking the parameter')
|
|
||||||
}
|
|
||||||
let fileSize = content.byteLength
|
|
||||||
key = SparkMD5.hash(key)
|
|
||||||
this.size = this.size + fileSize
|
|
||||||
console.log('setAsync fileSize ='+ fileSize +' all size ='+this.size + ' max size ='+this.maxSize);
|
|
||||||
this.putCacheMap(key, fileSize)
|
|
||||||
await FileUtils.getInstance().writeDataAsync(this.journalPath, DiskLruCache.SAVE + ' ' + key + FileReader.LF)
|
|
||||||
this.trimToSize()
|
|
||||||
let tempPath = this.path + key
|
|
||||||
await FileUtils.getInstance().writeNewFileAsync(tempPath, content)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 存储disk缓存数据
|
|
||||||
*
|
|
||||||
* @param key key 键值
|
|
||||||
* @param path 文件路径
|
|
||||||
*/
|
|
||||||
setFileByPath(key: string, path: string) {
|
|
||||||
if (!!!key) {
|
|
||||||
throw new Error('key is null, checking the parameter')
|
|
||||||
}
|
|
||||||
if (!!!path || !FileUtils.getInstance().exist(path)) {
|
|
||||||
throw new Error('path is null or no exist file, checking the parameter')
|
|
||||||
}
|
|
||||||
let fileSize = FileUtils.getInstance().getFileSize(path)
|
|
||||||
if (fileSize == -1) {
|
|
||||||
throw new Error('path getFileSize error ')
|
|
||||||
}
|
|
||||||
key = SparkMD5.hash(key)
|
|
||||||
this.size = this.size + fileSize
|
|
||||||
this.putCacheMap(key, fileSize)
|
|
||||||
FileUtils.getInstance().writeData(this.journalPath, DiskLruCache.SAVE + ' ' + key + FileReader.LF)
|
|
||||||
this.trimToSize()
|
|
||||||
fileSize = FileUtils.getInstance().getFileSize(path)
|
|
||||||
FileUtils.getInstance().copyFile(path, this.path + key)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步存储disk缓存数据
|
|
||||||
*
|
|
||||||
* @param key key 键值
|
|
||||||
* @param path 文件路径
|
|
||||||
*/
|
|
||||||
async setFileByPathAsync(key: string, path: string): Promise<void> {
|
|
||||||
if (!!!key) {
|
|
||||||
throw new Error('key is null, checking the parameter')
|
|
||||||
}
|
|
||||||
if (!!!path || !FileUtils.getInstance().exist(path)) {
|
|
||||||
throw new Error('path is null or no exist file, checking the parameter')
|
|
||||||
}
|
|
||||||
let fileSize = FileUtils.getInstance().getFileSize(path)
|
|
||||||
if (fileSize == -1) {
|
|
||||||
throw new Error('path getFileSize error ')
|
|
||||||
}
|
|
||||||
key = SparkMD5.hash(key)
|
|
||||||
this.size = this.size + fileSize
|
|
||||||
this.putCacheMap(key, fileSize)
|
|
||||||
await FileUtils.getInstance().writeDataAsync(this.journalPath, DiskLruCache.SAVE + ' ' + key + FileReader.LF)
|
|
||||||
this.trimToSize()
|
|
||||||
fileSize = FileUtils.getInstance().getFileSize(path)
|
|
||||||
await FileUtils.getInstance().copyFileAsync(path, this.path + key)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取key缓存数据
|
|
||||||
*
|
|
||||||
* @param key key 键值
|
|
||||||
*/
|
|
||||||
get(key: string): ArrayBuffer {
|
|
||||||
if (!!!key) {
|
|
||||||
throw new Error('key is null,checking the parameter');
|
|
||||||
}
|
|
||||||
key = SparkMD5.hash(key)
|
|
||||||
let path = this.path + key;
|
|
||||||
if (FileUtils.getInstance().exist(path)) {
|
|
||||||
let ab: ArrayBuffer = FileUtils.getInstance().readFile(path)
|
|
||||||
this.putCacheMap(key, ab.byteLength)
|
|
||||||
FileUtils.getInstance().writeData(this.journalPath, DiskLruCache.READ + ' ' + key + FileReader.LF)
|
|
||||||
return ab
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步获取key缓存数据
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
*/
|
|
||||||
async getAsync(key: string): Promise<ArrayBuffer> {
|
|
||||||
if (!!!key) {
|
|
||||||
throw new Error('key is null,checking the parameter');
|
|
||||||
}
|
|
||||||
key = SparkMD5.hash(key)
|
|
||||||
let path = this.path + key;
|
|
||||||
if (FileUtils.getInstance().exist(path)) {
|
|
||||||
let ab: ArrayBuffer = await FileUtils.getInstance().readFileAsync(path)
|
|
||||||
this.putCacheMap(key, ab.byteLength)
|
|
||||||
await FileUtils.getInstance().writeDataAsync(this.journalPath, DiskLruCache.READ + ' ' + key + FileReader.LF)
|
|
||||||
return ab
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取key缓存数据绝对路径
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
*/
|
|
||||||
getFileToPath(key: string): string {
|
|
||||||
if (!!!key) {
|
|
||||||
throw new Error('key is null,checking the parameter');
|
|
||||||
}
|
|
||||||
key = SparkMD5.hash(key);
|
|
||||||
let path = this.path + key;
|
|
||||||
if (FileUtils.getInstance().exist(path)) {
|
|
||||||
FileUtils.getInstance().writeData(this.journalPath, DiskLruCache.READ + ' ' + key + FileReader.LF);
|
|
||||||
return path
|
|
||||||
} else {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步获取key缓存数据绝对路径
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
*/
|
|
||||||
async getFileToPathAsync(key: string): Promise<string> {
|
|
||||||
if (!!!key) {
|
|
||||||
throw new Error('key is null,checking the parameter');
|
|
||||||
}
|
|
||||||
key = SparkMD5.hash(key);
|
|
||||||
let path = this.path + key;
|
|
||||||
if (FileUtils.getInstance().exist(path)) {
|
|
||||||
await FileUtils.getInstance().writeDataAsync(this.journalPath, DiskLruCache.READ + ' ' + key + FileReader.LF);
|
|
||||||
return path
|
|
||||||
} else {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除key缓存数据
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
*/
|
|
||||||
deleteCacheDataByKey(key: string): DiskCacheEntry {
|
|
||||||
if (!!!key) {
|
|
||||||
throw new Error('key is null,checking the parameter');
|
|
||||||
}
|
|
||||||
key = SparkMD5.hash(key)
|
|
||||||
let path = this.path + key;
|
|
||||||
if (FileUtils.getInstance().exist(path)) {
|
|
||||||
let ab = FileUtils.getInstance().readFile(path)
|
|
||||||
this.size = this.size - ab.byteLength
|
|
||||||
this.cacheMap.remove(key)
|
|
||||||
FileUtils.getInstance().writeData(this.journalPath, DiskLruCache.REMOVE + ' ' + key + FileReader.LF)
|
|
||||||
FileUtils.getInstance().deleteFile(path)
|
|
||||||
}
|
|
||||||
return this.cacheMap.get(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*遍历当前的磁盘缓存数据
|
|
||||||
*
|
|
||||||
* @param fn 遍历后方法回调
|
|
||||||
*/
|
|
||||||
foreachDiskLruCache(fn) {
|
|
||||||
this.cacheMap.each(fn)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清除所有disk缓存数据
|
|
||||||
*/
|
|
||||||
cleanCacheData() {
|
|
||||||
this.cacheMap.each((value, key) => {
|
|
||||||
FileUtils.getInstance().deleteFile(this.path + key)
|
|
||||||
})
|
|
||||||
FileUtils.getInstance().deleteFile(this.journalPath)
|
|
||||||
this.cacheMap.clear()
|
|
||||||
this.size = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重置journal文件数据
|
|
||||||
*/
|
|
||||||
private resetJournalFile() {
|
|
||||||
FileUtils.getInstance().clearFile(this.journalPath)
|
|
||||||
for (let key of this.cacheMap.keys()) {
|
|
||||||
FileUtils.getInstance().writeData(this.journalPath, DiskLruCache.SAVE + ' ' + key + FileReader.LF)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取journal文件的缓存数据
|
|
||||||
*
|
|
||||||
* @param path 日志缓存文件路径地址
|
|
||||||
*/
|
|
||||||
private readJournal(path: string) {
|
|
||||||
let fileReader = new FileReader(path)
|
|
||||||
let line: string = ''
|
|
||||||
while (!fileReader.isEnd()) {
|
|
||||||
line = fileReader.readLine()
|
|
||||||
line = line.replace(FileReader.LF, '').replace(FileReader.CR, '')
|
|
||||||
this.dealWithJournal(line)
|
|
||||||
}
|
|
||||||
fileReader.close()
|
|
||||||
FileUtils.getInstance().deleteFile(this.journalPathTemp)
|
|
||||||
this.trimToSize()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 缓存数据map集合
|
|
||||||
*
|
|
||||||
* @param key 键值
|
|
||||||
* @param length 缓存文件大小
|
|
||||||
*/
|
|
||||||
private putCacheMap(key: string, length?: number) {
|
|
||||||
if (length > 0) {
|
|
||||||
console.log('key = '+key)
|
|
||||||
console.log('value length= '+ length)
|
|
||||||
this.cacheMap.put(key, new DiskCacheEntry(key, length))
|
|
||||||
} else {
|
|
||||||
this.cacheMap.put(key, new DiskCacheEntry(key))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据LRU算法删除多余缓存数据
|
|
||||||
*/
|
|
||||||
private trimToSize() {
|
|
||||||
|
|
||||||
while (this.size > this.maxSize) {
|
|
||||||
let tempKey: string = this.cacheMap.getFirstKey()
|
|
||||||
let fileSize = FileUtils.getInstance().getFileSize(this.path + tempKey)
|
|
||||||
if (fileSize > 0) {
|
|
||||||
this.size = this.size - fileSize
|
|
||||||
}
|
|
||||||
console.log('trimToSize fileSize ='+ fileSize +' all size ='+this.size + ' max size ='+this.maxSize);
|
|
||||||
FileUtils.getInstance().deleteFile(this.path + tempKey)
|
|
||||||
this.cacheMap.remove(tempKey)
|
|
||||||
FileUtils.getInstance().writeData(this.journalPath, DiskLruCache.REMOVE + ' ' + tempKey + FileReader.LF)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getPath(){
|
|
||||||
return this.path;
|
|
||||||
}
|
|
||||||
|
|
||||||
getCacheMap(){
|
|
||||||
return this.cacheMap;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,83 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
import fileio from '@ohos.fileio'
|
|
||||||
|
|
||||||
export class FileReader {
|
|
||||||
// 换行符
|
|
||||||
static readonly LF: string = '\n'
|
|
||||||
|
|
||||||
// CR符
|
|
||||||
static readonly CR: string = '\r'
|
|
||||||
|
|
||||||
// 文件大小
|
|
||||||
fileLength: number = 0
|
|
||||||
|
|
||||||
// 读取的长度
|
|
||||||
length: number = 0
|
|
||||||
|
|
||||||
// 读写stream
|
|
||||||
stream: any = null
|
|
||||||
|
|
||||||
// 缓存buf
|
|
||||||
buf: ArrayBuffer = new ArrayBuffer(1)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取文件行
|
|
||||||
*
|
|
||||||
* @param path 文件路径
|
|
||||||
*/
|
|
||||||
constructor(path: string) {
|
|
||||||
if (!path || Object.keys(path).length == 0) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
this.stream = fileio.createStreamSync(path, 'r+');
|
|
||||||
let stat = fileio.statSync(path)
|
|
||||||
this.fileLength = stat.size
|
|
||||||
} catch (e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 循环读取文件数据
|
|
||||||
*/
|
|
||||||
readLine(): string {
|
|
||||||
let line = ''
|
|
||||||
while (this.length <= this.fileLength) {
|
|
||||||
this.stream.readSync(this.buf, { position: this.length })
|
|
||||||
this.length++
|
|
||||||
let temp = String.fromCharCode.apply(null, new Uint8Array(this.buf));
|
|
||||||
line = line + temp
|
|
||||||
if (temp == FileReader.LF || temp == FileReader.CR) {
|
|
||||||
return line
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return line
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断文件是否结束
|
|
||||||
*/
|
|
||||||
isEnd() {
|
|
||||||
return this.fileLength <= 0 || this.length == this.fileLength
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 关闭stream
|
|
||||||
*/
|
|
||||||
close() {
|
|
||||||
this.stream.closeSync()
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,240 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
import fileio from '@ohos.fileio'
|
|
||||||
|
|
||||||
export class FileUtils {
|
|
||||||
private static sInstance: FileUtils
|
|
||||||
static readonly SEPARATOR: string = '/'
|
|
||||||
base64Str: string = ''
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单例实现FileUtils类
|
|
||||||
*/
|
|
||||||
public static getInstance(): FileUtils {
|
|
||||||
if (!this.sInstance) {
|
|
||||||
this.sInstance = new FileUtils();
|
|
||||||
}
|
|
||||||
return this.sInstance;
|
|
||||||
}
|
|
||||||
|
|
||||||
private constructor() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 新建文件
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径及文件名
|
|
||||||
* @return number 文件句柄id
|
|
||||||
*/
|
|
||||||
createFile(path: string): number {
|
|
||||||
return fileio.openSync(path, 0o100, 0o664)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除文件
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径及文件名
|
|
||||||
*/
|
|
||||||
deleteFile(path: string): void {
|
|
||||||
fileio.unlinkSync(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 拷贝文件
|
|
||||||
*
|
|
||||||
* @param src 文件绝对路径及文件名
|
|
||||||
* @param dest 拷贝到对应的路径
|
|
||||||
*/
|
|
||||||
copyFile(src: string, dest: string) {
|
|
||||||
fileio.copyFileSync(src, dest);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步拷贝文件
|
|
||||||
*
|
|
||||||
* @param src 文件绝对路径及文件名
|
|
||||||
* @param dest 拷贝到对应的路径
|
|
||||||
*/
|
|
||||||
async copyFileAsync(src: string, dest: string): Promise<void> {
|
|
||||||
await fileio.copyFile(src, dest);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清空已有文件数据
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径
|
|
||||||
*/
|
|
||||||
clearFile(path: string): number {
|
|
||||||
return fileio.openSync(path, 0o1000)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 向path写入数据
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径
|
|
||||||
* @param content 文件内容
|
|
||||||
*/
|
|
||||||
writeData(path: string, content: ArrayBuffer | string) {
|
|
||||||
let fd = fileio.openSync(path, 0o102, 0o664)
|
|
||||||
let stat = fileio.statSync(path)
|
|
||||||
fileio.writeSync(fd, content, { position: stat.size })
|
|
||||||
fileio.closeSync(fd)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步向path写入数据
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径
|
|
||||||
* @param content 文件内容
|
|
||||||
*/
|
|
||||||
async writeDataAsync(path: string, content: ArrayBuffer | string): Promise<void> {
|
|
||||||
let fd = await fileio.open(path, 0o102, 0o664)
|
|
||||||
let stat = await fileio.stat(path)
|
|
||||||
await fileio.write(fd, content, { position: stat.size })
|
|
||||||
await fileio.close(fd)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断path文件是否存在
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径
|
|
||||||
*/
|
|
||||||
exist(path: string): boolean {
|
|
||||||
try {
|
|
||||||
let stat = fileio.statSync(path)
|
|
||||||
return stat.isFile()
|
|
||||||
} catch (e) {
|
|
||||||
console.error("FileUtils exist e " + e)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 向path写入数据
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径
|
|
||||||
* @param data 文件内容
|
|
||||||
*/
|
|
||||||
writeNewFile(path: string, data: ArrayBuffer) {
|
|
||||||
this.createFile(path)
|
|
||||||
this.writeFile(path, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 向path写入数据
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径
|
|
||||||
* @param data 文件内容
|
|
||||||
*/
|
|
||||||
async writeNewFileAsync(path: string, data: ArrayBuffer): Promise<void> {
|
|
||||||
await fileio.open(path, 0o100, 0o664)
|
|
||||||
let fd = await fileio.open(path, 0o102, 0o664)
|
|
||||||
await fileio.ftruncate(fd)
|
|
||||||
await fileio.write(fd, data)
|
|
||||||
await fileio.fsync(fd)
|
|
||||||
await fileio.close(fd)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取path的文件大小
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径
|
|
||||||
*/
|
|
||||||
getFileSize(path: string): number {
|
|
||||||
try {
|
|
||||||
let stat = fileio.statSync(path)
|
|
||||||
return stat.size
|
|
||||||
} catch (e) {
|
|
||||||
console.error("FileUtils getFileSize e " + e)
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取路径path的文件
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径
|
|
||||||
*/
|
|
||||||
readFile(path: string): ArrayBuffer {
|
|
||||||
let fd = fileio.openSync(path, 0o2);
|
|
||||||
let length = fileio.statSync(path).size
|
|
||||||
let buf = new ArrayBuffer(length);
|
|
||||||
fileio.readSync(fd, buf)
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取路径path的文件
|
|
||||||
*
|
|
||||||
* @param path 文件绝对路径
|
|
||||||
*/
|
|
||||||
async readFileAsync(path: string): Promise<ArrayBuffer> {
|
|
||||||
let stat = await fileio.stat(path);
|
|
||||||
let fd = await fileio.open(path, 0o2);
|
|
||||||
let length = stat.size;
|
|
||||||
let buf = new ArrayBuffer(length);
|
|
||||||
await fileio.read(fd, buf);
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建文件夹
|
|
||||||
*
|
|
||||||
* @param path 文件夹绝对路径,只有是权限范围内的路径,可以生成
|
|
||||||
* @param recursive
|
|
||||||
*/
|
|
||||||
createFolder(path: string, recursive?: boolean) {
|
|
||||||
if (recursive) {
|
|
||||||
if (!this.existFolder(path)) {
|
|
||||||
let lastInterval = path.lastIndexOf(FileUtils.SEPARATOR)
|
|
||||||
if (lastInterval == 0) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
let newPath = path.substring(0, lastInterval)
|
|
||||||
this.createFolder(newPath, true)
|
|
||||||
if (!this.existFolder(path)) {
|
|
||||||
fileio.mkdirSync(path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!this.existFolder(path)) {
|
|
||||||
fileio.mkdirSync(path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断文件夹是否存在
|
|
||||||
*
|
|
||||||
* @param path 文件夹绝对路径
|
|
||||||
*/
|
|
||||||
existFolder(path: string): boolean {
|
|
||||||
try {
|
|
||||||
let stat = fileio.statSync(path)
|
|
||||||
return stat.isDirectory()
|
|
||||||
} catch (e) {
|
|
||||||
console.error("FileUtils folder exist e " + e)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private writeFile(path: string, content: ArrayBuffer | string) {
|
|
||||||
let fd = fileio.openSync(path, 0o102, 0o664)
|
|
||||||
fileio.ftruncateSync(fd)
|
|
||||||
fileio.writeSync(fd, content)
|
|
||||||
fileio.fsyncSync(fd)
|
|
||||||
fileio.closeSync(fd)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,11 +0,0 @@
|
||||||
{
|
|
||||||
"module": {
|
|
||||||
"name": "disklrucache",
|
|
||||||
"type": "har",
|
|
||||||
"deviceTypes": [
|
|
||||||
"default",
|
|
||||||
"tablet"
|
|
||||||
],
|
|
||||||
"uiSyntax": "ets"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
{
|
|
||||||
"string": [
|
|
||||||
{
|
|
||||||
"name": "page_show",
|
|
||||||
"value": "page from npm package"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
import AbilityStage from "@ohos.application.AbilityStage"
|
|
||||||
|
|
||||||
export default class MyAbilityStage extends AbilityStage {
|
|
||||||
onCreate() {
|
|
||||||
console.log("[Demo] MyAbilityStage onCreate")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -111,7 +111,7 @@ struct CompressPage {
|
||||||
}
|
}
|
||||||
private cropressRecource() {
|
private cropressRecource() {
|
||||||
var data = new Array<Resource>();
|
var data = new Array<Resource>();
|
||||||
data.push($r('app.media.photo5'))
|
data.push($r('app.media.jpgSample'))
|
||||||
var rename: OnRenameListener = {
|
var rename: OnRenameListener = {
|
||||||
reName() {
|
reName() {
|
||||||
return "test_1.jpg";
|
return "test_1.jpg";
|
||||||
|
|
|
@ -23,7 +23,7 @@ import {PixelMapPack} from '@ohos/imageknife'
|
||||||
@Component
|
@Component
|
||||||
@Entry
|
@Entry
|
||||||
export struct CropImagePage {
|
export struct CropImagePage {
|
||||||
private _resource: Resource= $r('app.media.demo_org');
|
private _resource: Resource= $r('app.media.jpgSample');
|
||||||
@State x: number = 0;
|
@State x: number = 0;
|
||||||
@State y: number = 0;
|
@State y: number = 0;
|
||||||
@State crop_size: number = 100;
|
@State crop_size: number = 100;
|
||||||
|
|
|
@ -29,23 +29,23 @@ export struct CropImagePage2 {
|
||||||
@State options1: PixelMapCrop.Options = new PixelMapCrop.Options();
|
@State options1: PixelMapCrop.Options = new PixelMapCrop.Options();
|
||||||
@State cropTap: boolean = false;
|
@State cropTap: boolean = false;
|
||||||
@State pack: PixelMapPack = new PixelMapPack();
|
@State pack: PixelMapPack = new PixelMapPack();
|
||||||
@State width: number = 0;
|
@State width1: number = 0;
|
||||||
@State height: number = 0;
|
@State height1: number = 0;
|
||||||
@State _rotate: number = 0;
|
@State _rotate: number = 0;
|
||||||
@State _scale: number = 1;
|
@State _scale: number = 1;
|
||||||
private _resource: Resource = $r('app.media.bmpNet');
|
private _resource: Resource = $r('app.media.bmpSample');
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
Column() {
|
Column() {
|
||||||
|
|
||||||
Button('点击解析图片')
|
Button('点击解析图片')
|
||||||
.onClick(() => {
|
.onClick(() => {
|
||||||
globalThis.ImageKnife.getImageKnifeContext().resourceManager.getMedia($r('app.media.bmpNet').id)
|
globalThis.ImageKnife.getImageKnifeContext().resourceManager.getMedia($r('app.media.bmpSample').id)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
let arrayBuffer = FileUtils.getInstance().uint8ArrayToBuffer(data);
|
let arrayBuffer = FileUtils.getInstance().uint8ArrayToBuffer(data);
|
||||||
let optionx = new PixelMapCrop.Options();
|
let optionx = new PixelMapCrop.Options();
|
||||||
optionx.setWidth(600)
|
optionx.setWidth(800)
|
||||||
.setHeight(400)
|
.setHeight(800)
|
||||||
.setCropFunction((err, pixelmap, sx, sy) => {
|
.setCropFunction((err, pixelmap, sx, sy) => {
|
||||||
console.log('PMC setCropFunction callback')
|
console.log('PMC setCropFunction callback')
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -54,8 +54,8 @@ export struct CropImagePage2 {
|
||||||
let pack1 = new PixelMapPack();
|
let pack1 = new PixelMapPack();
|
||||||
pack1.pixelMap = pixelmap;
|
pack1.pixelMap = pixelmap;
|
||||||
this.pack = pack1;
|
this.pack = pack1;
|
||||||
this.width = sx * px2vp(1);
|
this.width1 = sx * px2vp(1);
|
||||||
this.height = sy * px2vp(1);
|
this.height1 = sy * px2vp(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,13 +77,13 @@ export struct CropImagePage2 {
|
||||||
|
|
||||||
|
|
||||||
Image(this.pack.pixelMap)
|
Image(this.pack.pixelMap)
|
||||||
.width(this.width)
|
.width(this.width1)
|
||||||
.height(this.height)
|
.height(this.height1)
|
||||||
.objectFit(ImageFit.Contain)
|
.objectFit(ImageFit.Contain)
|
||||||
.rotate({
|
.rotate({
|
||||||
z: 1,
|
z: 1,
|
||||||
centerX: this.width / 2,
|
centerX: this.width1 / 2,
|
||||||
centerY: this.height / 2,
|
centerY: this.height1 / 2,
|
||||||
angle: this._rotate
|
angle: this._rotate
|
||||||
})
|
})
|
||||||
.scale({ x: this._scale, y: this._scale, z: 1.0 })
|
.scale({ x: this._scale, y: this._scale, z: 1.0 })
|
||||||
|
|
|
@ -26,6 +26,7 @@ struct FrescoImageTestCasePage {
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.icon_loading'),
|
placeholderSrc: $r('app.media.icon_loading'),
|
||||||
errorholderSrc: $r('app.media.icon_failed'),
|
errorholderSrc: $r('app.media.icon_failed'),
|
||||||
|
retryholderSrc: $r('app.media.icon_retry'),
|
||||||
displayProgress: true,
|
displayProgress: true,
|
||||||
animateDuration: 300,
|
animateDuration: 300,
|
||||||
retryLoad:true
|
retryLoad:true
|
||||||
|
@ -36,6 +37,7 @@ struct FrescoImageTestCasePage {
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.icon_loading'),
|
placeholderSrc: $r('app.media.icon_loading'),
|
||||||
errorholderSrc: $r('app.media.icon_failed'),
|
errorholderSrc: $r('app.media.icon_failed'),
|
||||||
|
retryholderSrc: $r('app.media.icon_retry'),
|
||||||
displayProgress: true,
|
displayProgress: true,
|
||||||
animateDuration: 300,
|
animateDuration: 300,
|
||||||
retryLoad:true
|
retryLoad:true
|
||||||
|
@ -46,6 +48,7 @@ struct FrescoImageTestCasePage {
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.icon_loading'),
|
placeholderSrc: $r('app.media.icon_loading'),
|
||||||
errorholderSrc: $r('app.media.icon_failed'),
|
errorholderSrc: $r('app.media.icon_failed'),
|
||||||
|
retryholderSrc: $r('app.media.icon_retry'),
|
||||||
displayProgress: true,
|
displayProgress: true,
|
||||||
animateDuration: 300,
|
animateDuration: 300,
|
||||||
retryLoad:true
|
retryLoad:true
|
||||||
|
@ -56,6 +59,7 @@ struct FrescoImageTestCasePage {
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.icon_loading'),
|
placeholderSrc: $r('app.media.icon_loading'),
|
||||||
errorholderSrc: $r('app.media.icon_failed'),
|
errorholderSrc: $r('app.media.icon_failed'),
|
||||||
|
retryholderSrc: $r('app.media.icon_retry'),
|
||||||
displayProgress: true,
|
displayProgress: true,
|
||||||
animateDuration: 300,
|
animateDuration: 300,
|
||||||
retryLoad:true
|
retryLoad:true
|
||||||
|
|
|
@ -42,7 +42,7 @@ struct PngjTestCasePage {
|
||||||
this.rootFolder = globalThis.ImageKnife.getImageKnifeContext().filesDir;
|
this.rootFolder = globalThis.ImageKnife.getImageKnifeContext().filesDir;
|
||||||
|
|
||||||
|
|
||||||
globalThis.ImageKnife.getImageKnifeContext().resourceManager.getMedia($r('app.media.Tomato').id)
|
globalThis.ImageKnife.getImageKnifeContext().resourceManager.getMedia($r('app.media.pngSample').id)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
this.pngSource = FileUtils.getInstance().uint8ArrayToBuffer(data);
|
this.pngSource = FileUtils.getInstance().uint8ArrayToBuffer(data);
|
||||||
})
|
})
|
||||||
|
|
|
@ -24,8 +24,8 @@ struct TestAllTypeImageKnifeComponentPage {
|
||||||
{
|
{
|
||||||
loadSrc: $r('app.media.jpgSample'),
|
loadSrc: $r('app.media.jpgSample'),
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.Tomato'),
|
placeholderSrc: $r('app.media.jpgSample'),
|
||||||
errorholderSrc: $r('app.media.picture1'),
|
errorholderSrc: $r('app.media.pngSample'),
|
||||||
transform: {
|
transform: {
|
||||||
transformType:TransformType.RotateImageTransformation,
|
transformType:TransformType.RotateImageTransformation,
|
||||||
rotateImage:180
|
rotateImage:180
|
||||||
|
@ -35,8 +35,8 @@ struct TestAllTypeImageKnifeComponentPage {
|
||||||
{
|
{
|
||||||
loadSrc: $r('app.media.pngSample'),
|
loadSrc: $r('app.media.pngSample'),
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.Tomato'),
|
placeholderSrc: $r('app.media.jpgSample'),
|
||||||
errorholderSrc: $r('app.media.picture1'),
|
errorholderSrc: $r('app.media.pngSample'),
|
||||||
transform: {
|
transform: {
|
||||||
transformType:TransformType.RotateImageTransformation,
|
transformType:TransformType.RotateImageTransformation,
|
||||||
rotateImage:180
|
rotateImage:180
|
||||||
|
@ -46,8 +46,8 @@ struct TestAllTypeImageKnifeComponentPage {
|
||||||
{
|
{
|
||||||
loadSrc: $r('app.media.jpgSample'),
|
loadSrc: $r('app.media.jpgSample'),
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.Tomato'),
|
placeholderSrc: $r('app.media.jpgSample'),
|
||||||
errorholderSrc: $r('app.media.picture1'),
|
errorholderSrc: $r('app.media.pngSample'),
|
||||||
transform: {
|
transform: {
|
||||||
transformType:TransformType.RotateImageTransformation,
|
transformType:TransformType.RotateImageTransformation,
|
||||||
rotateImage:180
|
rotateImage:180
|
||||||
|
@ -57,8 +57,8 @@ struct TestAllTypeImageKnifeComponentPage {
|
||||||
{
|
{
|
||||||
loadSrc: $r('app.media.svgSample'),
|
loadSrc: $r('app.media.svgSample'),
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.Tomato'),
|
placeholderSrc: $r('app.media.jpgSample'),
|
||||||
errorholderSrc: $r('app.media.picture1'),
|
errorholderSrc: $r('app.media.pngSample'),
|
||||||
transform: {
|
transform: {
|
||||||
transformType:TransformType.RotateImageTransformation,
|
transformType:TransformType.RotateImageTransformation,
|
||||||
rotateImage:180
|
rotateImage:180
|
||||||
|
@ -68,8 +68,8 @@ struct TestAllTypeImageKnifeComponentPage {
|
||||||
{
|
{
|
||||||
loadSrc: $r('app.media.bmpSample'),
|
loadSrc: $r('app.media.bmpSample'),
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.Tomato'),
|
placeholderSrc: $r('app.media.jpgSample'),
|
||||||
errorholderSrc: $r('app.media.picture1'),
|
errorholderSrc: $r('app.media.pngSample'),
|
||||||
transform: {
|
transform: {
|
||||||
transformType:TransformType.RotateImageTransformation,
|
transformType:TransformType.RotateImageTransformation,
|
||||||
rotateImage:180
|
rotateImage:180
|
||||||
|
|
|
@ -39,7 +39,7 @@ import {PixelMapPack} from '@ohos/imageknife'
|
||||||
*/
|
*/
|
||||||
let mRotate: number = 0;
|
let mRotate: number = 0;
|
||||||
//let mUrl = "https://hbimg.huabanimg.com/cc6af25f8d782d3cf3122bef4e61571378271145735e9-vEVggB"
|
//let mUrl = "https://hbimg.huabanimg.com/cc6af25f8d782d3cf3122bef4e61571378271145735e9-vEVggB"
|
||||||
let mUrl = $r('app.media.check_big');
|
let mUrl = $r('app.media.pngSample');
|
||||||
|
|
||||||
@Entry
|
@Entry
|
||||||
@Component
|
@Component
|
||||||
|
@ -486,7 +486,7 @@ struct TransformPixelMapPage {
|
||||||
*/
|
*/
|
||||||
centerCrop() {
|
centerCrop() {
|
||||||
var imageKnifeOption = new RequestOption();
|
var imageKnifeOption = new RequestOption();
|
||||||
imageKnifeOption.load($r('app.media.photo5'))
|
imageKnifeOption.load($r('app.media.jpgSample'))
|
||||||
// imageKnifeOption.load(mUrl)
|
// imageKnifeOption.load(mUrl)
|
||||||
.addListener((err, data) => {
|
.addListener((err, data) => {
|
||||||
let result = new PixelMapPack();
|
let result = new PixelMapPack();
|
||||||
|
|
|
@ -25,8 +25,8 @@ struct Index {
|
||||||
{
|
{
|
||||||
loadSrc: "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83ericA1Mv66TwicuYOtbDMBcUhv1aa9RJBeAn9uURfcZD0AUGrJebAn1g2AjN0vb2E1XTET7fTuLBNmA/132",
|
loadSrc: "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83ericA1Mv66TwicuYOtbDMBcUhv1aa9RJBeAn9uURfcZD0AUGrJebAn1g2AjN0vb2E1XTET7fTuLBNmA/132",
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.Tomato'),
|
placeholderSrc: $r('app.media.jpgSample'),
|
||||||
errorholderSrc: $r('app.media.picture1'),
|
errorholderSrc: $r('app.media.pngSample'),
|
||||||
transform: {
|
transform: {
|
||||||
transformType:TransformType.RotateImageTransformation,
|
transformType:TransformType.RotateImageTransformation,
|
||||||
rotateImage:180
|
rotateImage:180
|
||||||
|
@ -36,8 +36,8 @@ struct Index {
|
||||||
{
|
{
|
||||||
loadSrc: "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83ericA1Mv66TwicuYOtbDMBcUhv1aa9RJBeAn9uURfcZD0AUGrJebAn1g2AjN0vb2E1XTET7fTuLBNmA/132",
|
loadSrc: "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83ericA1Mv66TwicuYOtbDMBcUhv1aa9RJBeAn9uURfcZD0AUGrJebAn1g2AjN0vb2E1XTET7fTuLBNmA/132",
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.Tomato'),
|
placeholderSrc: $r('app.media.jpgSample'),
|
||||||
errorholderSrc: $r('app.media.picture1'),
|
errorholderSrc: $r('app.media.pngSample'),
|
||||||
transform: {
|
transform: {
|
||||||
transformType:TransformType.RoundedCornersTransformation,
|
transformType:TransformType.RoundedCornersTransformation,
|
||||||
roundedCorners:{
|
roundedCorners:{
|
||||||
|
@ -53,8 +53,8 @@ struct Index {
|
||||||
{
|
{
|
||||||
loadSrc: $r('app.media.pngSample'),
|
loadSrc: $r('app.media.pngSample'),
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.Tomato'),
|
placeholderSrc: $r('app.media.jpgSample'),
|
||||||
errorholderSrc: $r('app.media.picture1'),
|
errorholderSrc: $r('app.media.pngSample'),
|
||||||
transform: {
|
transform: {
|
||||||
transformType:TransformType.RotateImageTransformation,
|
transformType:TransformType.RotateImageTransformation,
|
||||||
rotateImage:180
|
rotateImage:180
|
||||||
|
@ -64,8 +64,8 @@ struct Index {
|
||||||
{
|
{
|
||||||
loadSrc: $r('app.media.jpgSample'),
|
loadSrc: $r('app.media.jpgSample'),
|
||||||
size: { width: 300, height: 300 },
|
size: { width: 300, height: 300 },
|
||||||
placeholderSrc: $r('app.media.Tomato'),
|
placeholderSrc: $r('app.media.jpgSample'),
|
||||||
errorholderSrc: $r('app.media.picture1'),
|
errorholderSrc: $r('app.media.pngSample'),
|
||||||
transform: {
|
transform: {
|
||||||
transformType:TransformType.RoundedCornersTransformation,
|
transformType:TransformType.RoundedCornersTransformation,
|
||||||
roundedCorners:{
|
roundedCorners:{
|
||||||
|
|
Before Width: | Height: | Size: 74 KiB |
Before Width: | Height: | Size: 558 B After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 117 KiB |
Before Width: | Height: | Size: 117 KiB |
Before Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 104 KiB |
Before Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 112 KiB |
Before Width: | Height: | Size: 212 KiB |
Before Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 80 KiB |
Before Width: | Height: | Size: 214 KiB |
Before Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.0 KiB |
Before Width: | Height: | Size: 498 KiB |
Before Width: | Height: | Size: 540 B |
Before Width: | Height: | Size: 111 KiB |
Before Width: | Height: | Size: 122 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 768 KiB |
Before Width: | Height: | Size: 9.6 MiB After Width: | Height: | Size: 1.8 MiB |
Before Width: | Height: | Size: 1.9 MiB |
Before Width: | Height: | Size: 1.4 MiB |
Before Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 68 KiB |
Before Width: | Height: | Size: 119 KiB After Width: | Height: | Size: 2.4 MiB |
Before Width: | Height: | Size: 99 KiB |
Before Width: | Height: | Size: 396 KiB After Width: | Height: | Size: 9.0 MiB |
Before Width: | Height: | Size: 6.7 KiB After Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 2.4 KiB |
After Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 462 KiB |
Before Width: | Height: | Size: 563 KiB After Width: | Height: | Size: 3.7 MiB |
Before Width: | Height: | Size: 7.6 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 512 KiB |
Before Width: | Height: | Size: 423 KiB |
Before Width: | Height: | Size: 68 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 2.4 MiB After Width: | Height: | Size: 1.6 MiB |
|
@ -1,17 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<svg width="750px" height="551px" viewBox="0 0 750 551" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
||||||
<title>蒙版</title>
|
|
||||||
<defs>
|
|
||||||
<linearGradient x1="39.5134336%" y1="125.874399%" x2="39.5134336%" y2="2.34648164%" id="linearGradient-1">
|
|
||||||
<stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
|
|
||||||
<stop stop-color="#00FF28" offset="100%"></stop>
|
|
||||||
</linearGradient>
|
|
||||||
</defs>
|
|
||||||
<g id="知更问题反馈" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
|
||||||
<g id="多层切图问题" transform="translate(-539.000000, -975.000000)" fill="url(#linearGradient-1)">
|
|
||||||
<g id="编组-2备份-3" transform="translate(539.000000, 975.000000)">
|
|
||||||
<rect id="蒙版" x="0" y="0" width="750" height="551"></rect>
|
|
||||||
</g>
|
|
||||||
</g>
|
|
||||||
</g>
|
|
||||||
</svg>
|
|
Before Width: | Height: | Size: 932 B |
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 251 KiB |
|
@ -1,17 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<svg width="750px" height="551px" viewBox="0 0 750 551" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
||||||
<title>蒙版</title>
|
|
||||||
<defs>
|
|
||||||
<linearGradient x1="39.5134336%" y1="125.874399%" x2="39.5134336%" y2="2.34648164%" id="linearGradient-1">
|
|
||||||
<stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
|
|
||||||
<stop stop-color="#00FF28" offset="100%"></stop>
|
|
||||||
</linearGradient>
|
|
||||||
</defs>
|
|
||||||
<g id="知更问题反馈" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
|
||||||
<g id="多层切图问题" transform="translate(-539.000000, -975.000000)" fill="url(#linearGradient-1)">
|
|
||||||
<g id="编组-2备份-3" transform="translate(539.000000, 975.000000)">
|
|
||||||
<rect id="蒙版" x="0" y="0" width="750" height="551"></rect>
|
|
||||||
</g>
|
|
||||||
</g>
|
|
||||||
</g>
|
|
||||||
</svg>
|
|
Before Width: | Height: | Size: 932 B |
Before Width: | Height: | Size: 42 KiB |
Before Width: | Height: | Size: 7.5 KiB |
After Width: | Height: | Size: 496 KiB |
|
@ -1,29 +1,29 @@
|
||||||
{
|
{
|
||||||
"types":"",
|
"types": "",
|
||||||
"keywords":[
|
"keywords": [
|
||||||
"OpenHarmony",
|
"OpenHarmony",
|
||||||
"ImageKnife",
|
"ImageKnife",
|
||||||
"glide"
|
"glide"
|
||||||
],
|
],
|
||||||
"author":"ohos_tpc",
|
"author": "ohos_tpc",
|
||||||
"description":"专门为OpenHarmony打造的一款图像加载缓存库,致力于更高效、更轻便、更简单",
|
"description": "专门为OpenHarmony打造的一款图像加载缓存库,致力于更高效、更轻便、更简单",
|
||||||
"ohos":{
|
"ohos": {
|
||||||
"org":"opensource"
|
"org": "opensource"
|
||||||
},
|
},
|
||||||
"main":"index.ets",
|
"main": "index.ets",
|
||||||
"repository":"https://gitee.com/openharmony-tpc/ImageKnife",
|
"repository": "https://gitee.com/openharmony-tpc/ImageKnife",
|
||||||
"version":"1.0.2",
|
"version": "1.0.3",
|
||||||
"dependencies":{
|
"dependencies": {
|
||||||
"pako":"^1.0.5",
|
"pako": "^1.0.5",
|
||||||
"@ohos/disklrucache":"file:../disklrucache",
|
"@ohos/disklrucache": "^1.0.0",
|
||||||
"crc-32":"^1.2.0"
|
"crc-32": "^1.2.0"
|
||||||
},
|
},
|
||||||
"tags":[
|
"tags": [
|
||||||
"OpenHarmony",
|
"OpenHarmony",
|
||||||
"ImageKnife",
|
"ImageKnife",
|
||||||
"glide"
|
"glide"
|
||||||
],
|
],
|
||||||
"license":"Apache License 2.0",
|
"license": "Apache License 2.0",
|
||||||
"devDependencies":{},
|
"devDependencies": {},
|
||||||
"name":"@ohos/imageknife"
|
"name": "@ohos/imageknife"
|
||||||
}
|
}
|
|
@ -61,7 +61,7 @@ export class ImageKnife {
|
||||||
this.memoryCache = new LruCache<string, any>(100);
|
this.memoryCache = new LruCache<string, any>(100);
|
||||||
|
|
||||||
// 创建disk缓存 传入的size 为多少比特 比如20KB 传入20*1024
|
// 创建disk缓存 传入的size 为多少比特 比如20KB 传入20*1024
|
||||||
this.diskMemoryCache = DiskLruCache.create(this.imageKnifeContext.filesDir + ImageKnife.SEPARATOR + this.diskCacheFolder, 30 * 1024 * 1024);
|
this.diskMemoryCache = DiskLruCache.create(this.imageKnifeContext);
|
||||||
|
|
||||||
// 创建网络下载能力
|
// 创建网络下载能力
|
||||||
this.dataFetch = new DownloadClient();
|
this.dataFetch = new DownloadClient();
|
||||||
|
@ -180,12 +180,10 @@ export class ImageKnife {
|
||||||
|
|
||||||
// 替代原来的DiskLruCache
|
// 替代原来的DiskLruCache
|
||||||
public replaceDiskLruCache(size:number) {
|
public replaceDiskLruCache(size:number) {
|
||||||
// this.diskMemoryCache = DiskLruCache.create(this.imageKnifeContext.filesDir+ImageKnife.SEPARATOR+this.diskCacheFolder, size);
|
|
||||||
|
|
||||||
if (this.diskMemoryCache.getCacheMap().size() <= 0) {
|
if (this.diskMemoryCache.getCacheMap().size() <= 0) {
|
||||||
this.diskMemoryCache = DiskLruCache.create(this.imageKnifeContext.filesDir + ImageKnife.SEPARATOR + this.diskCacheFolder, size);
|
this.diskMemoryCache = DiskLruCache.create(this.imageKnifeContext, size);
|
||||||
} else {
|
} else {
|
||||||
let newDiskLruCache = DiskLruCache.create(this.imageKnifeContext.filesDir + ImageKnife.SEPARATOR + this.diskCacheFolder, size);
|
let newDiskLruCache = DiskLruCache.create(this.imageKnifeContext, size);
|
||||||
this.diskMemoryCache.foreachDiskLruCache(function (value, key, map) {
|
this.diskMemoryCache.foreachDiskLruCache(function (value, key, map) {
|
||||||
newDiskLruCache.set(key, value);
|
newDiskLruCache.set(key, value);
|
||||||
})
|
})
|
||||||
|
|
|
@ -23,7 +23,7 @@ import {PixelMapPack} from '../imageknife/PixelMapPack'
|
||||||
export struct ImageKnifeComponent {
|
export struct ImageKnifeComponent {
|
||||||
@Watch('watchImageKnifeOption') @Link imageKnifeOption: ImageKnifeOption;
|
@Watch('watchImageKnifeOption') @Link imageKnifeOption: ImageKnifeOption;
|
||||||
@State imageKnifePixelMapPack: PixelMapPack = new PixelMapPack();
|
@State imageKnifePixelMapPack: PixelMapPack = new PixelMapPack();
|
||||||
@State imageKnifeResource: Resource = $r('app.media.icon_loading')
|
@State imageKnifeResource: Resource = undefined
|
||||||
@State imageKnifeString: string = ''
|
@State imageKnifeString: string = ''
|
||||||
@State normalPixelMap: boolean = false;
|
@State normalPixelMap: boolean = false;
|
||||||
@State normalResource: boolean = true;
|
@State normalResource: boolean = true;
|
||||||
|
@ -40,6 +40,8 @@ export struct ImageKnifeComponent {
|
||||||
@State imageWidth: string = '100%';
|
@State imageWidth: string = '100%';
|
||||||
@State imageHeight: string = '100%';
|
@State imageHeight: string = '100%';
|
||||||
|
|
||||||
|
@State imageKnifeRetry: Resource = undefined;
|
||||||
|
|
||||||
hasRetry:boolean = false;
|
hasRetry:boolean = false;
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
|
@ -52,7 +54,7 @@ export struct ImageKnifeComponent {
|
||||||
.width(this.percentWidth)
|
.width(this.percentWidth)
|
||||||
.height(this.percentHeight)
|
.height(this.percentHeight)
|
||||||
|
|
||||||
Image($r('app.media.icon_retry'))
|
Image(this.imageKnifeRetry)
|
||||||
.onClick(()=>{
|
.onClick(()=>{
|
||||||
this.retryClick();
|
this.retryClick();
|
||||||
})
|
})
|
||||||
|
@ -144,6 +146,9 @@ export struct ImageKnifeComponent {
|
||||||
this.animateTo('image');
|
this.animateTo('image');
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if (this.imageKnifeOption.retryholderSrc) {
|
||||||
|
this.imageKnifeRetry = this.imageKnifeOption.retryholderSrc
|
||||||
|
}
|
||||||
if (this.imageKnifeOption.transform) {
|
if (this.imageKnifeOption.transform) {
|
||||||
this.requestAddTransform(request)
|
this.requestAddTransform(request)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,9 @@ export class ImageKnifeOption {
|
||||||
// 失败占位图
|
// 失败占位图
|
||||||
errorholderSrc?: PixelMap | Resource;
|
errorholderSrc?: PixelMap | Resource;
|
||||||
|
|
||||||
|
// 重试占位图
|
||||||
|
retryholderSrc?: Resource;
|
||||||
|
|
||||||
// 缩略图,范围(0,1)
|
// 缩略图,范围(0,1)
|
||||||
thumbSizeMultiplier?: number;
|
thumbSizeMultiplier?: number;
|
||||||
|
|
||||||
|
|
|
@ -83,10 +83,7 @@ export class MaskTransformation implements BaseTransform<PixelMap> {
|
||||||
if (!this._mResourceData) {
|
if (!this._mResourceData) {
|
||||||
throw new Error("MaskTransformation resource is empty");
|
throw new Error("MaskTransformation resource is empty");
|
||||||
}
|
}
|
||||||
resmgr.getResourceManager()
|
globalThis.ImageKnife.getImageKnifeContext().resourceManager.getMedia(this._mResourceData.id)
|
||||||
.then(result => {
|
|
||||||
result.getMedia(this._mResourceData
|
|
||||||
.id)
|
|
||||||
.then(array => {
|
.then(array => {
|
||||||
let buffer = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset);
|
let buffer = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset);
|
||||||
var imageSource = image.createImageSource(buffer as any);
|
var imageSource = image.createImageSource(buffer as any);
|
||||||
|
@ -105,6 +102,5 @@ export class MaskTransformation implements BaseTransform<PixelMap> {
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
func("MaskTransformation openInternal error" + err, null);
|
func("MaskTransformation openInternal error" + err, null);
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Before Width: | Height: | Size: 6.7 KiB |
Before Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 2.4 KiB |