优化文件缓存和内存释放以及成功回调新增宽高参数
Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
commit
08399f557e
|
@ -5,6 +5,7 @@
|
|||
- 子线程写入文件缓存获取buffer优化
|
||||
- 成功回调增加返回图片分辨率宽高
|
||||
- 内存缓存时将pixelMap进行release释放
|
||||
- 提供清理缓存能力
|
||||
|
||||
## 3.0.0-rc.4
|
||||
- 支持hsp多包图片资源
|
||||
|
|
|
@ -185,6 +185,8 @@ ImageKnifeComponent({ ImageKnifeOption:
|
|||
| deleteHeader | key: string | 全局删除http请求头 |
|
||||
| setEngineKeyImpl | IEngineKey | 全局配置缓存key生成策略 |
|
||||
| putCacheImage | url: string, pixelMap: PixelMap, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string | 写入内存磁盘缓存 |
|
||||
| removeMemoryCache| url: string | ImageKnifeOption | 清理指定内存缓存 |
|
||||
| removeFileCache | url: string | ImageKnifeOption | 清理指定磁盘缓存 |
|
||||
|
||||
## 约束与限制
|
||||
|
||||
|
|
|
@ -120,6 +120,12 @@ struct Index {
|
|||
})
|
||||
})
|
||||
|
||||
Button('测试移除图片缓存接口').margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TestRemoveCache',
|
||||
});
|
||||
})
|
||||
|
||||
}
|
||||
.width('100%')
|
||||
|
||||
|
|
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* Copyright (C) 2024 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 { ImageKnifeComponent, ImageKnife, ImageKnifeOption, CacheStrategy } from '@ohos/imageknife'
|
||||
|
||||
@Entry
|
||||
@Component
|
||||
struct TestRemoveCache {
|
||||
@State imageKnifeOption: ImageKnifeOption = {
|
||||
loadSrc: $r('app.media.startIcon'),
|
||||
placeholderSrc: $r('app.media.loading'),
|
||||
errorholderSrc:$r('app.media.failed')
|
||||
}
|
||||
@State source: PixelMap | string | Resource = $r("app.media.startIcon");
|
||||
@State source1: PixelMap | string | Resource = $r("app.media.startIcon");
|
||||
@State url: string = '';
|
||||
|
||||
build() {
|
||||
Column() {
|
||||
Flex() {
|
||||
Button("预加载gif图").onClick(() => {
|
||||
this.imageKnifeOption.loadSrc = "https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658";
|
||||
this.url = "https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658";
|
||||
})
|
||||
.margin({left:10})
|
||||
Button("内存缓存获取gif").onClick(() => {
|
||||
ImageKnife.getInstance()
|
||||
.getCacheImage("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658",
|
||||
CacheStrategy.Memory)
|
||||
.then((data) => {
|
||||
this.source = data !== undefined ? data.source : $r("app.media.startIcon");
|
||||
})
|
||||
})
|
||||
.margin({left:10})
|
||||
Button("文件缓存获取gif").onClick(() => {
|
||||
ImageKnife.getInstance()
|
||||
.getCacheImage("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658",
|
||||
CacheStrategy.File)
|
||||
.then((data) => {
|
||||
this.source1 = data !== undefined ? data.source : $r("app.media.startIcon");
|
||||
})
|
||||
})
|
||||
.margin({left:10})
|
||||
}
|
||||
|
||||
Flex() {
|
||||
Button("预加载静态图").onClick(() => {
|
||||
this.imageKnifeOption.loadSrc = 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp';
|
||||
this.url = 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp';
|
||||
})
|
||||
.margin({left:10})
|
||||
Button("内存缓存获取").onClick(() => {
|
||||
ImageKnife.getInstance()
|
||||
.getCacheImage('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp',
|
||||
CacheStrategy.Memory)
|
||||
.then((data) => {
|
||||
this.source = data!.source;
|
||||
})
|
||||
})
|
||||
.margin({left:10})
|
||||
Button("文件缓存获取").onClick(() => {
|
||||
ImageKnife.getInstance()
|
||||
.getCacheImage('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp',
|
||||
CacheStrategy.File)
|
||||
.then((data) => {
|
||||
this.source1 = data!.source;
|
||||
})
|
||||
})
|
||||
.margin({left:10})
|
||||
}.margin({ top: 10 })
|
||||
|
||||
Flex() {
|
||||
Button("删除全部缓存").onClick(() => {
|
||||
ImageKnife.getInstance()
|
||||
.removeAllMemoryCache()
|
||||
ImageKnife.getInstance()
|
||||
.removeAllFileCache()
|
||||
})
|
||||
.margin({left:5})
|
||||
Button("删除全部内存缓存").onClick(() => {
|
||||
ImageKnife.getInstance()
|
||||
.removeAllMemoryCache()
|
||||
})
|
||||
.margin({left:5})
|
||||
Button("删除全部文件缓存").onClick(() => {
|
||||
ImageKnife.getInstance()
|
||||
.removeAllFileCache()
|
||||
})
|
||||
.margin({left:5})
|
||||
}.margin({ top: 10 })
|
||||
|
||||
Flex() {
|
||||
Button("删除自定义内存缓存").onClick(() => {
|
||||
ImageKnife.getInstance()
|
||||
.removeMemoryCache(this.url)
|
||||
})
|
||||
.margin({left:20})
|
||||
Button("删除自定义文件缓存").onClick(() => {
|
||||
ImageKnife.getInstance()
|
||||
.removeFileCache(this.url)
|
||||
})
|
||||
.margin({left:20})
|
||||
}.margin({ top: 10 })
|
||||
|
||||
ImageKnifeComponent({
|
||||
imageKnifeOption: this.imageKnifeOption
|
||||
}).width(200).height(200).margin({ top: 10 })
|
||||
Image(this.source).margin({ top: 10 })
|
||||
.width(200).height(200)
|
||||
Image(this.source1).margin({ top: 10 })
|
||||
.width(200).height(200)
|
||||
}
|
||||
.height('100%').width('100%')
|
||||
}
|
||||
}
|
|
@ -16,6 +16,7 @@
|
|||
"pages/ObjectFitPage",
|
||||
"pages/TestWriteCacheStage",
|
||||
"pages/LoadStatePage",
|
||||
"pages/TestHspPreLoadImage"
|
||||
"pages/TestHspPreLoadImage",
|
||||
"pages/TestRemoveCache"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (C) 2024 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
|
||||
import { ImageKnife, ImageKnifeOption } from '@ohos/imageknife';
|
||||
import { ImageKnifeRequestSource } from '@ohos/imageknife/src/main/ets/model/ImageKnifeData';
|
||||
|
||||
export default function ImageKnifeTest() {
|
||||
describe('ImageKnifeTest', () => {
|
||||
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
||||
beforeAll(() => {
|
||||
// Presets an action, which is performed only once before all test cases of the test suite start.
|
||||
// This API supports only one parameter: preset action function.
|
||||
});
|
||||
beforeEach(() => {
|
||||
// Presets an action, which is performed before each unit test case starts.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: preset action function.
|
||||
});
|
||||
afterEach(() => {
|
||||
// Presets a clear action, which is performed after each unit test case ends.
|
||||
// The number of execution times is the same as the number of test cases defined by **it**.
|
||||
// This API supports only one parameter: clear action function.
|
||||
});
|
||||
afterAll(() => {
|
||||
// Presets a clear action, which is performed after all test cases of the test suite end.
|
||||
// This API supports only one parameter: clear action function.
|
||||
});
|
||||
|
||||
it('removeMemoryCache', 0, async () => {
|
||||
let a = 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp';
|
||||
let option: ImageKnifeOption = {
|
||||
loadSrc: 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp',
|
||||
signature: ''
|
||||
}
|
||||
let key = ImageKnife.getInstance()
|
||||
.getEngineKeyImpl()
|
||||
.generateMemoryKey('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp', ImageKnifeRequestSource.SRC, option)
|
||||
ImageKnife.getInstance()
|
||||
.removeMemoryCache(a)
|
||||
expect(ImageKnife.getInstance()
|
||||
.loadFromMemoryCache(key)).assertEqual(undefined)
|
||||
});
|
||||
|
||||
it('removeFileCache', 0, async () => {
|
||||
let a = 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp';
|
||||
let key = ImageKnife.getInstance()
|
||||
.getEngineKeyImpl()
|
||||
.generateFileKey('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp');
|
||||
ImageKnife.getInstance()
|
||||
.removeFileCache(a)
|
||||
expect(ImageKnife.getInstance()
|
||||
.loadFromFileCache(key)).assertEqual(undefined)
|
||||
});
|
||||
});
|
||||
}
|
|
@ -13,10 +13,10 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
|
||||
import { ImageKnifeOption} from '../main/ets/ImageKnifeOption';
|
||||
import { ImageKnifeOption } from "@ohos/imageknife"
|
||||
|
||||
export default function imageKnifeOptionTest() {
|
||||
describe('imageKnifeOptionTest',() => {
|
||||
export default function ImageKnifeOptionTest() {
|
||||
describe('ImageKnifeOptionTest',() => {
|
||||
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
||||
beforeAll(() => {
|
||||
// Presets an action, which is performed only once before all test cases of the test suite start.
|
|
@ -14,10 +14,14 @@
|
|||
*/
|
||||
import DefaultJobQueueTest from './DefaultJobQueueTest.test';
|
||||
import FileLruCacheTest from './FileLruCache.test';
|
||||
import ImageKnifeOptionTest from './ImageKnifeOption.test';
|
||||
import MemoryLruCacheTest from './MemoryLruCache.test';
|
||||
import ImageKnifeTest from './ImageKnife.test';
|
||||
|
||||
export default function testsuite() {
|
||||
MemoryLruCacheTest();
|
||||
FileLruCacheTest();
|
||||
DefaultJobQueueTest();
|
||||
ImageKnifeOptionTest();
|
||||
ImageKnifeTest();
|
||||
}
|
|
@ -107,6 +107,19 @@ export class ImageKnife {
|
|||
removeAllMemoryCache(): void {
|
||||
this.memoryCache.removeAll()
|
||||
}
|
||||
/*
|
||||
* 清除指定内存缓存
|
||||
* */
|
||||
removeMemoryCache(url: string | ImageKnifeOption) {
|
||||
let imageKnifeOption = new ImageKnifeOption();
|
||||
if (typeof url == 'string') {
|
||||
imageKnifeOption.loadSrc = url;
|
||||
} else {
|
||||
imageKnifeOption = url;
|
||||
}
|
||||
let key = this.getEngineKeyImpl().generateMemoryKey(imageKnifeOption.loadSrc, ImageKnifeRequestSource.SRC, imageKnifeOption);
|
||||
this.memoryCache.remove(key);
|
||||
}
|
||||
|
||||
loadFromMemoryCache(key: string): ImageKnifeData | undefined {
|
||||
if (key !== "") {
|
||||
|
@ -281,6 +294,23 @@ export class ImageKnife {
|
|||
await this.fileCache.removeAll()
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 清除指定文件缓存
|
||||
* */
|
||||
removeFileCache(url: string | ImageKnifeOption) {
|
||||
let imageKnifeOption: ImageKnifeOption;
|
||||
if (url instanceof ImageKnifeOption) {
|
||||
imageKnifeOption = url;
|
||||
} else {
|
||||
imageKnifeOption = {
|
||||
loadSrc: url
|
||||
};
|
||||
}
|
||||
let key = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature);
|
||||
if (this.fileCache !== undefined) {
|
||||
this.fileCache.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
saveWithoutWriteFile(key: string, bufferSize: number): void {
|
||||
this.fileCache?.putWithoutWriteFile(key, bufferSize)
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import imageKnifeOptionTest from './ImageKnifeOption.test';
|
||||
import localUnitTest from './LocalUnit.test';
|
||||
|
||||
export default function testsuite() {
|
||||
localUnitTest();
|
||||
imageKnifeOptionTest();
|
||||
}
|
Loading…
Reference in New Issue