新增reload加载接口和网络恢复重新加载样例
Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
parent
1787fffea7
commit
aa2cd9c1bf
29
README_zh.md
29
README_zh.md
|
@ -311,20 +311,21 @@ ImageKnifeAnimatorComponent({
|
|||
|
||||
### ImageKnife接口
|
||||
|
||||
| 参数名称 | 入参内容 | 功能简介 |
|
||||
|------------------|-------------------------------------------------------------------------------------------------------|---------------|
|
||||
| initMemoryCache | newMemoryCache: IMemoryCache | 自定义内存缓存策略 |
|
||||
| initFileCache | context: Context, size: number, memory: number | 初始化文件缓存数量和大小 |
|
||||
| preLoadCache | loadSrc: string I ImageKnifeOption | 预加载并返回文件缓存路径 |
|
||||
| getCacheImage | loadSrc: string, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string) | 从内存或文件缓存中获取资源 |
|
||||
| addHeader | key: string, value: Object | 全局添加http请求头 |
|
||||
| setHeaderOptions | Array<HeaderOptions> | 全局设置http请求头 |
|
||||
| deleteHeader | key: string | 全局删除http请求头 |
|
||||
| setCustomGetImage | customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined> | 全局设置自定义下载 |
|
||||
| setEngineKeyImpl | IEngineKey | 全局配置缓存key生成策略 |
|
||||
| putCacheImage | url: string, pixelMap: PixelMap, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string | 写入内存磁盘缓存 |
|
||||
| removeMemoryCache| url: string | ImageKnifeOption | 清理指定内存缓存 |
|
||||
| removeFileCache | url: string | ImageKnifeOption | 清理指定磁盘缓存 |
|
||||
| 参数名称 | 入参内容 | 功能简介 |
|
||||
|-------------------|-------------------------------------------------------------------------------------------------------|------------------|
|
||||
| initMemoryCache | newMemoryCache: IMemoryCache | 自定义内存缓存策略 |
|
||||
| initFileCache | context: Context, size: number, memory: number | 初始化文件缓存数量和大小 |
|
||||
| reload | request: ImageKnifeRequest | 图片重新加载 |
|
||||
| preLoadCache | loadSrc: string I ImageKnifeOption | 预加载并返回文件缓存路径 |
|
||||
| getCacheImage | loadSrc: string, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string) | 从内存或文件缓存中获取资源 |
|
||||
| addHeader | key: string, value: Object | 全局添加http请求头 |
|
||||
| setHeaderOptions | Array<HeaderOptions> | 全局设置http请求头 |
|
||||
| deleteHeader | key: string | 全局删除http请求头 |
|
||||
| setCustomGetImage | customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined> | 全局设置自定义下载 |
|
||||
| setEngineKeyImpl | IEngineKey | 全局配置缓存key生成策略 |
|
||||
| putCacheImage | url: string, pixelMap: PixelMap, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string | 写入内存磁盘缓存 |
|
||||
| removeMemoryCache | url: string | ImageKnifeOption | 清理指定内存缓存 |
|
||||
| removeFileCache | url: string | ImageKnifeOption | 清理指定磁盘缓存 |
|
||||
### 图形变换类型(需要为GPUImage添加依赖项)
|
||||
|
||||
| 类型 | 相关描述 |
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* 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 { connection } from '@kit.NetworkKit'
|
||||
import { List } from '@kit.ArkTS'
|
||||
import { ImageKnifeRequest,ImageKnife,ImageKnifeComponent } from '@ohos/libraryimageknife'
|
||||
|
||||
@Entry
|
||||
@Component
|
||||
struct ImageKnifeReload {
|
||||
aboutToAppear(): void {
|
||||
NetWatchState.init()
|
||||
}
|
||||
build() {
|
||||
Column() {
|
||||
ImageKnifeComponent({
|
||||
imageKnifeOption:{
|
||||
loadSrc:"https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp",
|
||||
placeholderSrc:$r('app.media.loading'),
|
||||
errorholderSrc:$r('app.media.failed'),
|
||||
onLoadListener:{
|
||||
onLoadFailed(err,request){
|
||||
NetWatchState.requestList.add(request)
|
||||
}
|
||||
}
|
||||
}
|
||||
}).width(200).height(200)
|
||||
ImageKnifeComponent({
|
||||
imageKnifeOption:{
|
||||
loadSrc:"https://img-blog.csdn.net/20140514114029140",
|
||||
placeholderSrc:$r('app.media.loading'),
|
||||
errorholderSrc:$r('app.media.failed'),
|
||||
onLoadListener:{
|
||||
onLoadFailed(err,request){
|
||||
NetWatchState.requestList.add(request)
|
||||
}
|
||||
}
|
||||
}
|
||||
}).width(200).height(200).margin({top:10})
|
||||
}.width("100%")
|
||||
.height("100%")
|
||||
}
|
||||
}
|
||||
|
||||
class NetWatchState {
|
||||
public static netConnection: connection.NetConnection | undefined = undefined
|
||||
public static requestList: List<ImageKnifeRequest> = new List()
|
||||
|
||||
static init() {
|
||||
NetWatchState.netConnection = connection.createNetConnection()
|
||||
// 注册订阅事件
|
||||
NetWatchState.netConnection.register(()=>{
|
||||
})
|
||||
// 订阅网络能力变化事件。调用register后,才能接收到此事件通知
|
||||
NetWatchState.netConnection.on("netCapabilitiesChange",(data:connection.NetCapabilityInfo)=>{
|
||||
if(NetWatchState.requestList.length > 0 && data.netHandle.netId >= 100) {
|
||||
NetWatchState.requestList.forEach((request)=>{
|
||||
ImageKnife.getInstance().reload(request)
|
||||
NetWatchState.requestList.remove(request)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
static cancel(){
|
||||
if(NetWatchState.netConnection != undefined) {
|
||||
// 取消订阅
|
||||
NetWatchState.netConnection.unregister(()=>{})
|
||||
}
|
||||
}
|
||||
}
|
|
@ -35,6 +35,11 @@ struct Index {
|
|||
uri: 'pages/ImageAnimatorPage',
|
||||
});
|
||||
})
|
||||
Button("网络恢复reload").margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/ImageKnifeReload',
|
||||
});
|
||||
})
|
||||
Button($r('app.string.Test_multiple_images')).margin({top:10}).onClick(()=>{
|
||||
router.push({
|
||||
uri: 'pages/TestCommonImage',
|
||||
|
|
|
@ -43,6 +43,15 @@
|
|||
],
|
||||
"when": "always"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "ohos.permission.GET_NETWORK_INFO",
|
||||
"usedScene": {
|
||||
"abilities": [
|
||||
"EntryAbility"
|
||||
],
|
||||
"when": "always"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
"pages/ImageAnimatorPage",
|
||||
"pages/TestSetCustomImagePage",
|
||||
"pages/TestErrorHolderPage",
|
||||
"pages/TestTaskResourcePage"
|
||||
"pages/TestTaskResourcePage",
|
||||
"pages/ImageKnifeReload"
|
||||
]
|
||||
}
|
|
@ -12,7 +12,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { ImageKnifeRequest } from './model/ImageKnifeRequest';
|
||||
import { ImageKnifeRequest, ImageKnifeRequestState } from './model/ImageKnifeRequest';
|
||||
import { CacheStrategy, ImageKnifeData, ImageKnifeRequestSource } from './model/ImageKnifeData';
|
||||
import { MemoryLruCache } from './cache/MemoryLruCache';
|
||||
import { IMemoryCache } from './cache/IMemoryCache'
|
||||
|
@ -80,7 +80,15 @@ export class ImageKnife {
|
|||
public isFileCacheInit(): boolean {
|
||||
return this.fileCache === undefined ? false : this.fileCache.isFileCacheInit()
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新加载
|
||||
*/
|
||||
reload(request: ImageKnifeRequest) {
|
||||
if (request.requestState == ImageKnifeRequestState.ERROR) {
|
||||
request.requestState = ImageKnifeRequestState.PROGRESS
|
||||
ImageKnife.getInstance().execute(request)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 全局添加单个请求头header
|
||||
* @param key 请求头属性
|
||||
|
|
|
@ -260,7 +260,7 @@ export class ImageKnifeDispatcher {
|
|||
if (requestWithSource.source === ImageKnifeRequestSource.SRC &&
|
||||
requestWithSource.request.imageKnifeOption.onLoadListener?.onLoadFailed !== undefined &&
|
||||
requestJobResult.loadFail) {
|
||||
requestWithSource.request.imageKnifeOption.onLoadListener.onLoadFailed(requestJobResult.loadFail);
|
||||
requestWithSource.request.imageKnifeOption.onLoadListener.onLoadFailed(requestJobResult.loadFail,requestWithSource.request);
|
||||
LogUtil.log("ImageKnife_DataTime_getAndShowImage_onLoadFailed:"+currentRequest.imageKnifeOption.loadSrc)
|
||||
}
|
||||
if (requestWithSource.source === ImageKnifeRequestSource.SRC &&
|
||||
|
|
|
@ -17,6 +17,7 @@ import common from '@ohos.app.ability.common'
|
|||
import { CacheStrategy, ImageKnifeData,EventImage } from './ImageKnifeData';
|
||||
import { PixelMapTransformation } from '../transform/PixelMapTransformation';
|
||||
import { drawing } from '@kit.ArkGraphics2D';
|
||||
import { ImageKnifeRequest } from './ImageKnifeRequest';
|
||||
|
||||
export interface HeaderOptions {
|
||||
key: string;
|
||||
|
@ -89,7 +90,7 @@ export interface OnLoadCallBack {
|
|||
onLoadSuccess?: (data: string | PixelMap | undefined, imageKnifeData: ImageKnifeData) => void;
|
||||
|
||||
// 请求结束
|
||||
onLoadFailed?: (err: string) => void;
|
||||
onLoadFailed?: (err: string,request?: ImageKnifeRequest) => void;
|
||||
// 请求取消
|
||||
onLoadCancel?: (reason: string) => void;
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 10 KiB |
Loading…
Reference in New Issue