1.ArkTs整改10 整改imageknife->requestmanager

Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
zhoulisheng1 2023-09-19 16:29:48 +08:00
parent a0b6e19f8a
commit 76ab03262f
10 changed files with 104 additions and 97 deletions

View File

@ -47,7 +47,11 @@ import { ToonFilterTransform } from '../imageknife/transform/ToonFilterTransform
import { VignetteFilterTransform } from '../imageknife/transform/VignetteFilterTransform' import { VignetteFilterTransform } from '../imageknife/transform/VignetteFilterTransform'
import { LogUtil } from '../imageknife/utils/LogUtil' import { LogUtil } from '../imageknife/utils/LogUtil'
import { ImageKnifeGlobal } from './ImageKnifeGlobal' import { ImageKnifeGlobal } from './ImageKnifeGlobal'
import { BusinessError } from '@ohos.base'
export interface Size {
width: number,
height: number
}
export class RequestOption { export class RequestOption {
loadSrc: string | PixelMap | Resource; loadSrc: string | PixelMap | Resource;
strategy: DiskStrategy = new AUTOMATIC(); strategy: DiskStrategy = new AUTOMATIC();
@ -119,10 +123,7 @@ export class RequestOption {
/** /**
* set image Component size * set image Component size
*/ */
setImageViewSize(imageSize: { setImageViewSize(imageSize:Size) {
width: number,
height: number
}) {
this.size.width = imageSize.width; this.size.width = imageSize.width;
this.size.height = imageSize.height; this.size.height = imageSize.height;
return this; return this;
@ -386,7 +387,7 @@ export class RequestOption {
// 缩略图解析成功 // 缩略图解析成功
thumbholderOnComplete(imageKnifeData: ImageKnifeData) { thumbholderOnComplete = (imageKnifeData: ImageKnifeData)=> {
if (!this.loadMainReady && !(this.loadErrorReady || this.loadRetryReady)) { if (!this.loadMainReady && !(this.loadErrorReady || this.loadRetryReady)) {
//主图未加载成功,并且未加载失败 显示占位图 主图加载成功或者加载失败后=>不展示占位图 //主图未加载成功,并且未加载失败 显示占位图 主图加载成功或者加载失败后=>不展示占位图
this.thumbHolderFunc.asyncSuccess(imageKnifeData) this.thumbHolderFunc.asyncSuccess(imageKnifeData)
@ -394,7 +395,7 @@ export class RequestOption {
} }
// 缩略图解析失败 // 缩略图解析失败
thumbholderOnError(error) { thumbholderOnError=(error? :BusinessError|string)=>{
LogUtil.log("缩略图解析失败 error =" + error) LogUtil.log("缩略图解析失败 error =" + error)
} }
@ -423,7 +424,7 @@ export class RequestOption {
LogUtil.log("重试占位图解析失败 error =" + error) LogUtil.log("重试占位图解析失败 error =" + error)
} }
loadComplete(imageKnifeData: ImageKnifeData) { loadComplete = (imageKnifeData: ImageKnifeData)=>{
this.loadMainReady = true; this.loadMainReady = true;
// 三级缓存数据加载成功 // 三级缓存数据加载成功
for (let requestListener of this.requestListeners) { for (let requestListener of this.requestListeners) {
@ -445,11 +446,11 @@ export class RequestOption {
} }
// 图片文件落盘之后会自动去寻找下一个数据加载 // 图片文件落盘之后会自动去寻找下一个数据加载
removeCurrentAndSearchNext(){ removeCurrentAndSearchNext =()=>{
(ImageKnifeGlobal.getInstance().getImageKnife()).removeRunning(this); (ImageKnifeGlobal.getInstance().getImageKnife()).removeRunning(this);
} }
loadError(err) { loadError = (err:BusinessError|string)=>{
LogUtil.log("loadError:" + err); LogUtil.log("loadError:" + err);
//失败占位图展示规则 //失败占位图展示规则
if (this.retryholderFunc) { if (this.retryholderFunc) {

View File

@ -16,7 +16,7 @@
import {ImageKnifeData} from "../../imageknife/ImageKnifeData" import {ImageKnifeData} from "../../imageknife/ImageKnifeData"
export interface MemoryCacheInfo{ export interface MemoryCacheInfo{
key: string, key: string,
data: ImageKnifeData data: ImageKnifeData | undefined
} }
export interface ResourceCacheInfo{ export interface ResourceCacheInfo{
path: string, path: string,
@ -27,9 +27,9 @@ export interface DataCacheInfo{
key: string key: string
} }
export interface AllCacheInfo { export interface AllCacheInfo {
memoryCacheInfo: MemoryCacheInfo memoryCacheInfo: MemoryCacheInfo | undefined
resourceCacheInfo: ResourceCacheInfo resourceCacheInfo: ResourceCacheInfo | undefined
dataCacheInfo: DataCacheInfo dataCacheInfo: DataCacheInfo | undefined
} }
export interface IAllCacheInfoCallback { export interface IAllCacheInfoCallback {

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import type { ICache } from "../requestmanage/ICache" import { ICache } from "../requestmanage/ICache"
import { DiskLruCache } from "@ohos/disklrucache" import { DiskLruCache } from "@ohos/disklrucache"
export class DiskCacheProxy implements ICache<string, ArrayBuffer> { export class DiskCacheProxy implements ICache<string, ArrayBuffer> {
@ -47,7 +47,7 @@ export class DiskCacheProxy implements ICache<string, ArrayBuffer> {
removeValue(key: string): ArrayBuffer{ removeValue(key: string): ArrayBuffer{
// Disk暂无实现 // Disk暂无实现
return; return new ArrayBuffer(0);
} }
clear() { clear() {

View File

@ -18,11 +18,11 @@ export interface ICache<K, V> {
// 缓存类型 // 缓存类型
getName(): string getName(): string
getValue(key: K): V; getValue(key: K): V|undefined;
putValue(key: K, value: V); putValue(key: K, value: V);
removeValue(key: K): V; removeValue(key: K): V|undefined;
clear(); clear();

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import type {ICache} from "../requestmanage/ICache" import {ICache} from "../requestmanage/ICache"
import {LruCache} from "../../cache/LruCache" import {LruCache} from "../../cache/LruCache"
export class MemoryCacheProxy <K, V> implements ICache<K, V> { export class MemoryCacheProxy <K, V> implements ICache<K, V> {

View File

@ -13,28 +13,26 @@
* limitations under the License. * limitations under the License.
*/ */
import { RequestOption } from '../../imageknife/RequestOption' import { RequestOption,Size } from '../../imageknife/RequestOption'
import { DiskLruCache } from '@ohos/disklrucache' import { DiskLruCache } from '@ohos/disklrucache'
import { LruCache } from '../../cache/LruCache' import { LruCache } from '../../cache/LruCache'
import { SparkMD5 } from '../../3rd_party/sparkmd5/spark-md5' import { SparkMD5 } from '../../3rd_party/sparkmd5/spark-md5'
import { MemoryCacheProxy } from '../requestmanage/MemoryCacheProxy' import { MemoryCacheProxy } from '../requestmanage/MemoryCacheProxy'
import { DiskCacheProxy } from '../requestmanage/DiskCacheProxy' import { DiskCacheProxy } from '../requestmanage/DiskCacheProxy'
import { FileTypeUtil } from '../utils/FileTypeUtil' import { FileTypeUtil } from '../utils/FileTypeUtil'
import type { IDataFetch } from '../../imageknife/networkmanage/IDataFetch' import { IDataFetch } from '../../imageknife/networkmanage/IDataFetch'
import type { IResourceFetch } from '../../imageknife/resourcemanage/IResourceFetch' import { IResourceFetch } from '../../imageknife/resourcemanage/IResourceFetch'
import { ImageKnifeData, ImageKnifeType } from '../ImageKnifeData' import { ImageKnifeData, ImageKnifeType } from '../ImageKnifeData'
import { AllCacheInfo } from '../../imageknife/interface/IAllCacheInfoCallback' import { AllCacheInfo } from '../../imageknife/interface/IAllCacheInfoCallback'
import { ParseImageUtil } from '../utils/ParseImageUtil' import { ParseImageUtil } from '../utils/ParseImageUtil'
import type { IParseImage } from '../interface/IParseImage' import { IParseImage } from '../interface/IParseImage'
import image from '@ohos.multimedia.image' import image from '@ohos.multimedia.image'
import { SVGParseImpl } from '../utils/svg/SVGParseImpl' import { SVGParseImpl } from '../utils/svg/SVGParseImpl'
import { GIFParseImpl } from '../utils/gif/GIFParseImpl' import { GIFParseImpl } from '../utils/gif/GIFParseImpl'
import { GIFFrame } from '../utils/gif/GIFFrame' import { GIFFrame } from '../utils/gif/GIFFrame'
import { LogUtil } from '../../imageknife/utils/LogUtil' import { LogUtil } from '../../imageknife/utils/LogUtil'
import { BusinessError } from '@ohos.base'
export interface AsyncString {
(data: string): void;
}
export enum Stage { export enum Stage {
@ -64,13 +62,13 @@ export enum RunReason {
export class RequestManager { export class RequestManager {
private TAG: string = "RequestManager"; private TAG: string = "RequestManager";
private options: RequestOption; private options: RequestOption;
private mMemoryCacheProxy: MemoryCacheProxy<string, any>; private mMemoryCacheProxy: MemoryCacheProxy<string, ImageKnifeData>;
private mDiskCacheProxy: DiskCacheProxy; private mDiskCacheProxy: DiskCacheProxy;
private mIDataFetch: IDataFetch; private mIDataFetch: IDataFetch;
private mIResourceFetch: IResourceFetch; private mIResourceFetch: IResourceFetch;
private mParseImageUtil: IParseImage; private mParseImageUtil: IParseImage<PixelMap>;
constructor(option: RequestOption, memoryCache1: LruCache<string, any>, diskMemoryCache1: DiskLruCache, dataFetch: IDataFetch, resourceFetch: IResourceFetch) { constructor(option: RequestOption, memoryCache1: LruCache<string, ImageKnifeData>, diskMemoryCache1: DiskLruCache, dataFetch: IDataFetch, resourceFetch: IResourceFetch) {
this.options = option; this.options = option;
// 缓存部分 // 缓存部分
@ -87,23 +85,27 @@ export class RequestManager {
this.mParseImageUtil = new ParseImageUtil(); this.mParseImageUtil = new ParseImageUtil();
} }
static execute(option: RequestOption, memoryCache1: LruCache<string, any>, diskMemoryCache1: DiskLruCache, dataFetch: IDataFetch, resourceFetch: IResourceFetch) { static execute(option: RequestOption, memoryCache1: LruCache<string, ImageKnifeData>, diskMemoryCache1: DiskLruCache, dataFetch: IDataFetch, resourceFetch: IResourceFetch) {
LogUtil.log("RequestManager execute") LogUtil.log("RequestManager execute")
let manager = new RequestManager(option, memoryCache1, diskMemoryCache1, dataFetch, resourceFetch); let manager = new RequestManager(option, memoryCache1, diskMemoryCache1, dataFetch, resourceFetch);
return new Promise<PixelMap>(manager.process.bind(manager)) return new Promise<ImageKnifeData>(manager.process)
.then(option.loadComplete.bind(option)) .then(option.loadComplete)
.then(manager.loadCompleteAfter.bind(manager)) .then(manager.loadCompleteAfter)
.catch(option.loadError.bind(option)); .catch(option.loadError);
} }
loadCompleteAfter() { loadCompleteAfter =()=>{
try { // 内部消化问题 try { // 内部消化问题
LogUtil.log("loadCompleteAfter!") LogUtil.log("loadCompleteAfter!")
if (this.options.allCacheInfoCallback) { if (this.options.allCacheInfoCallback) {
LogUtil.log("RequestOption =" + JSON.stringify(this.options)); LogUtil.log("RequestOption =" + JSON.stringify(this.options));
// 内存缓存 // 内存缓存
let allCacheInfo = new AllCacheInfo(); let allCacheInfo:AllCacheInfo = {
memoryCacheInfo:{key:'', data:new ImageKnifeData()},
resourceCacheInfo:{key:'', path:''},
dataCacheInfo:{key:'',path:''}
};
let memoryCache = this.mMemoryCacheProxy.getValue(this.options.generateCacheKey); let memoryCache = this.mMemoryCacheProxy.getValue(this.options.generateCacheKey);
allCacheInfo.memoryCacheInfo = { allCacheInfo.memoryCacheInfo = {
key: this.options.generateCacheKey, key: this.options.generateCacheKey,
@ -133,12 +135,12 @@ export class RequestManager {
private mStage: Stage = Stage.INITIALIZE; private mStage: Stage = Stage.INITIALIZE;
private mRunReason: RunReason = RunReason.INITIALIZE; private mRunReason: RunReason = RunReason.INITIALIZE;
process(onComplete, onError) { process = (onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void)=>{
LogUtil.log("RequestManager process !"); LogUtil.log("RequestManager process !");
this.loadLeve1MemoryCache(onComplete, onError) this.loadLeve1MemoryCache(onComplete, onError)
} }
private runWrapped(request: RequestOption, runReason: RunReason, onComplete, onError) { private runWrapped(request: RequestOption, runReason: RunReason, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
LogUtil.log("RequestManager runWrapped") LogUtil.log("RequestManager runWrapped")
if (runReason == RunReason.INITIALIZE) { if (runReason == RunReason.INITIALIZE) {
this.mStage = this.getNextStage(request, this.mStage); this.mStage = this.getNextStage(request, this.mStage);
@ -169,7 +171,7 @@ export class RequestManager {
} }
//究竟从哪里加载数据 //究竟从哪里加载数据
private searchLoadFrom(request: RequestOption, current: Stage, onComplete, onError) { private searchLoadFrom(request: RequestOption, current: Stage, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
LogUtil.log("RequestManager searchLoadFrom") LogUtil.log("RequestManager searchLoadFrom")
if (current == Stage.RESOURCE_CACHE) { if (current == Stage.RESOURCE_CACHE) {
this.loadDiskFromTransform(request, onComplete, onError); this.loadDiskFromTransform(request, onComplete, onError);
@ -185,8 +187,8 @@ export class RequestManager {
} }
// 加载网络资源 // 加载网络资源
private loadSourceFromNetwork(request: RequestOption, onComplete, onError) { private loadSourceFromNetwork(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
let success = (arraybuffer) => { let success = (arraybuffer:ArrayBuffer) => {
this.downloadSuccess(arraybuffer, onComplete, onError) this.downloadSuccess(arraybuffer, onComplete, onError)
} }
let error = (errorMsg:string) =>{ let error = (errorMsg:string) =>{
@ -196,10 +198,10 @@ export class RequestManager {
} }
// 加载本地资源 // 加载本地资源
private loadSourceFormNative(request: RequestOption, onComplete, onError) { private loadSourceFormNative(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
LogUtil.log("RequestManager loadSourceFormNative") LogUtil.log("RequestManager loadSourceFormNative")
// 本地解析后进行一级缓存 // 本地解析后进行一级缓存
let success = (arrayBuffer) => { let success = (arrayBuffer:ArrayBuffer) => {
// 使用媒体子系统 ImageSource解析文件 获取PixelMap // 使用媒体子系统 ImageSource解析文件 获取PixelMap
let fileTypeUtil = new FileTypeUtil(); let fileTypeUtil = new FileTypeUtil();
let typeValue = fileTypeUtil.getFileType(arrayBuffer) let typeValue = fileTypeUtil.getFileType(arrayBuffer)
@ -217,7 +219,7 @@ export class RequestManager {
}) })
} else { } else {
if (request.transformations[0]) { if (request.transformations[0]) {
request.transformations[0].transform(arrayBuffer, request, (error, pixelMap: PixelMap) => { request.transformations[0].transform(arrayBuffer, request, {asyncTransform:(error:BusinessError|string, pixelMap: PixelMap) => {
// 输出给Image // 输出给Image
if (pixelMap) { if (pixelMap) {
@ -227,7 +229,7 @@ export class RequestManager {
} else { } else {
onError(error); onError(error);
} }
}) }})
} }
else { else {
let success = (value: PixelMap) => { let success = (value: PixelMap) => {
@ -242,7 +244,7 @@ export class RequestManager {
this.mIResourceFetch.loadResource(request.loadSrc as Resource, success, onError); this.mIResourceFetch.loadResource(request.loadSrc as Resource, success, onError);
} }
// 加载磁盘缓存 原图 // 加载磁盘缓存 原图
private loadDiskFromSource(request: RequestOption, onComplete, onError) { private loadDiskFromSource(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
LogUtil.log("RequestManager loadDiskFromSource") LogUtil.log("RequestManager loadDiskFromSource")
let cached = this.mDiskCacheProxy.getValue(request.generateDataKey) let cached = this.mDiskCacheProxy.getValue(request.generateDataKey)
if (cached != null && cached.byteLength > 0) { if (cached != null && cached.byteLength > 0) {
@ -254,7 +256,7 @@ export class RequestManager {
} }
// 加载磁盘缓存 变换后图片 // 加载磁盘缓存 变换后图片
private loadDiskFromTransform(request: RequestOption, onComplete, onError) { private loadDiskFromTransform(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
LogUtil.log("RequestManager loadDiskFromTransform") LogUtil.log("RequestManager loadDiskFromTransform")
let cached = this.mDiskCacheProxy.getValue(request.generateResourceKey) let cached = this.mDiskCacheProxy.getValue(request.generateResourceKey)
if (cached != null) { if (cached != null) {
@ -265,7 +267,7 @@ export class RequestManager {
} }
} }
parseSource(request: RequestOption, onComplete, onError) { parseSource(request: RequestOption, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
LogUtil.log("RequestManager parseSource") LogUtil.log("RequestManager parseSource")
if ((typeof (request.loadSrc as image.PixelMap).isEditable) == 'boolean') { if ((typeof (request.loadSrc as image.PixelMap).isEditable) == 'boolean') {
// PixelMap 外层捕获效率更高,不会进入这里 // PixelMap 外层捕获效率更高,不会进入这里
@ -282,7 +284,7 @@ export class RequestManager {
} }
private loadLeve1MemoryCache(onComplete, onError) { private loadLeve1MemoryCache(onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
LogUtil.log("RequestManager loadLeve1MemoryCache") LogUtil.log("RequestManager loadLeve1MemoryCache")
// 一级缓存 内存获取 // 一级缓存 内存获取
let cache = this.mMemoryCacheProxy.loadMemoryCache(this.options.generateCacheKey, this.options.isCacheable); let cache = this.mMemoryCacheProxy.loadMemoryCache(this.options.generateCacheKey, this.options.isCacheable);
@ -297,7 +299,7 @@ export class RequestManager {
} }
// 解析磁盘文件变成PixeMap // 解析磁盘文件变成PixeMap
private parseDiskFile2PixelMap(request: RequestOption, source: ArrayBuffer, onComplete, onError) { private parseDiskFile2PixelMap(request: RequestOption, source: ArrayBuffer, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
// 步骤一文件转为pixelMap 然后变换 给Image组件 // 步骤一文件转为pixelMap 然后变换 给Image组件
let fileTypeUtil = new FileTypeUtil(); let fileTypeUtil = new FileTypeUtil();
let typeValue = fileTypeUtil.getFileType(source); let typeValue = fileTypeUtil.getFileType(source);
@ -314,23 +316,23 @@ export class RequestManager {
} else { } else {
if (this.options.transformations[0]) { if (this.options.transformations[0]) {
if (this.options.thumbSizeMultiplier) { if (this.options.thumbSizeMultiplier) {
let thumbOption = new RequestOption(); let thumbOption:RequestOption = new RequestOption();
thumbOption.setImageViewSize({ thumbOption.setImageViewSize({
width: Math.round(this.options.thumbSizeMultiplier * this.options.size.width), width: Math.round(this.options.thumbSizeMultiplier * this.options.size.width),
height: Math.round(this.options.thumbSizeMultiplier * this.options.size.height) height: Math.round(this.options.thumbSizeMultiplier * this.options.size.height)
}) })
let thumbCallback = this.options.thumbholderOnComplete.bind(this.options); let thumbCallback = this.options.thumbholderOnComplete;
let thumbError = this.options.thumbholderOnError.bind(this.options); let thumbError = this.options.thumbholderOnError;
this.options.transformations[0].transform(source, thumbOption, (error, pixelMap: PixelMap) => { this.options.transformations[0].transform(source, thumbOption,{asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => {
if (pixelMap) { if (pixelMap) {
let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap); let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap);
thumbCallback(imageKnifeData); thumbCallback(imageKnifeData);
} else { } else {
thumbError(error); thumbError(error);
} }
}) }})
setTimeout(()=>{ setTimeout(()=>{
this.options.transformations[0].transform(source, request, (error, pixelMap: PixelMap) => { this.options.transformations[0].transform(source, request, {asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => {
if (pixelMap) { if (pixelMap) {
// 保存一份变换后的图片PixelMap到MemoryCache // 保存一份变换后的图片PixelMap到MemoryCache
let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap); let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap);
@ -339,11 +341,11 @@ export class RequestManager {
} else { } else {
onError(error); onError(error);
} }
}) }})
},this.options.thumbDelayTime); },this.options.thumbDelayTime);
} }
else { else {
this.options.transformations[0].transform(source, request, (error, pixelMap: PixelMap) => { this.options.transformations[0].transform(source, request, {asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => {
if (pixelMap) { if (pixelMap) {
// 保存一份变换后的图片PixelMap到MemoryCache // 保存一份变换后的图片PixelMap到MemoryCache
let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap); let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap);
@ -352,13 +354,13 @@ export class RequestManager {
} else { } else {
onError(error); onError(error);
} }
}) }})
} }
} else { } else {
// thumbnail 缩略图部分 // thumbnail 缩略图部分
if (request.thumbSizeMultiplier) { if (request.thumbSizeMultiplier) {
let thumbCallback = this.options.thumbholderOnComplete.bind(this.options); let thumbCallback = this.options.thumbholderOnComplete
let thumbError = this.options.thumbholderOnError.bind(this.options); let thumbError = this.options.thumbholderOnError
let thumbSuccess = (value: PixelMap) => { let thumbSuccess = (value: PixelMap) => {
let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value); let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value);
thumbCallback(imageKnifeData); thumbCallback(imageKnifeData);
@ -386,13 +388,13 @@ export class RequestManager {
} }
// 解析磁盘变换后文件变成PixeMap // 解析磁盘变换后文件变成PixeMap
private parseDiskTransformFile2PixelMap(request: RequestOption, source: ArrayBuffer, onComplete, onError) { private parseDiskTransformFile2PixelMap(request: RequestOption, source: ArrayBuffer, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
let fileTypeUtil = new FileTypeUtil(); let fileTypeUtil = new FileTypeUtil();
let typeValue = fileTypeUtil.getFileType(source); let typeValue = fileTypeUtil.getFileType(source);
// thumbnail 缩略图部分 // thumbnail 缩略图部分
if (request.thumbSizeMultiplier) { if (request.thumbSizeMultiplier) {
let thumbCallback = this.options.thumbholderOnComplete.bind(this.options); let thumbCallback = this.options.thumbholderOnComplete
let thumbError = this.options.thumbholderOnError.bind(this.options); let thumbError = this.options.thumbholderOnError
let thumbSuccess = (value: PixelMap) => { let thumbSuccess = (value: PixelMap) => {
let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value); let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value);
thumbCallback(imageKnifeData); thumbCallback(imageKnifeData);
@ -416,7 +418,7 @@ export class RequestManager {
} }
} }
private downloadSuccess(source: ArrayBuffer, onComplete, onError) { private downloadSuccess(source: ArrayBuffer, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void) {
LogUtil.log('Download task completed.'); LogUtil.log('Download task completed.');
if(source == null || source == undefined || source.byteLength <= 0){ if(source == null || source == undefined || source.byteLength <= 0){
@ -447,8 +449,8 @@ export class RequestManager {
.then(async (arraybuffer: ArrayBuffer)=>{ .then(async (arraybuffer: ArrayBuffer)=>{
await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer) await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer)
}) })
.catch(err=>{ .catch( (err:BusinessError)=>{
LogUtil.log('download file is ='+ImageKnifeData.GIF+'and save diskLruCache error ='+ err) LogUtil.log('download file is ='+ImageKnifeData.GIF+'and save diskLruCache error ='+ (err as BusinessError))
}) })
}else if(ImageKnifeData.SVG == filetype){ }else if(ImageKnifeData.SVG == filetype){
// 处理svg // 处理svg
@ -461,8 +463,8 @@ export class RequestManager {
.then(async (arraybuffer: ArrayBuffer)=>{ .then(async (arraybuffer: ArrayBuffer)=>{
await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer) await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer)
}) })
.catch(err=>{ .catch((err:BusinessError)=>{
LogUtil.log('download file is ='+ImageKnifeData.SVG+'and save diskLruCache error ='+ err) LogUtil.log('download file is ='+ImageKnifeData.SVG+'and save diskLruCache error ='+ (err as BusinessError))
}) })
} else { } else {
// 进行变换 // 进行变换
@ -471,19 +473,19 @@ export class RequestManager {
if (this.options.thumbSizeMultiplier) { if (this.options.thumbSizeMultiplier) {
this.thumbnailProcess(source, filetype, onComplete, onError); this.thumbnailProcess(source, filetype, onComplete, onError);
} else { } else {
this.options.transformations[0].transform(source, this.options, (error, pixelMap: PixelMap) => { this.options.transformations[0].transform(source, this.options, {asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => {
if (pixelMap) { if (pixelMap) {
this.saveCacheAndDisk(pixelMap, filetype, onComplete, source); this.saveCacheAndDisk(pixelMap, filetype, onComplete, source);
} else { } else {
onError(error); onError(error);
} }
}) }})
} }
} else { } else {
// thumbnail 缩略图部分 // thumbnail 缩略图部分
if (this.options.thumbSizeMultiplier) { if (this.options.thumbSizeMultiplier) {
let thumbCallback = this.options.thumbholderOnComplete.bind(this.options); let thumbCallback = this.options.thumbholderOnComplete
let thumbError = this.options.thumbholderOnError.bind(this.options); let thumbError = this.options.thumbholderOnError
let thumbSuccess = (value: PixelMap) => { let thumbSuccess = (value: PixelMap) => {
let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value); let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value);
thumbCallback(imageKnifeData); thumbCallback(imageKnifeData);
@ -515,16 +517,16 @@ export class RequestManager {
private saveCacheAndDisk(value: PixelMap, filetype:string, onComplete, source:ArrayBuffer) { private saveCacheAndDisk(value: PixelMap, filetype:string, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, source:ArrayBuffer) {
let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value); let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, value);
this.mMemoryCacheProxy.putValue(this.options.generateCacheKey, imageKnifeData); this.mMemoryCacheProxy.putValue(this.options.generateCacheKey, imageKnifeData);
let save2DiskCache = async (arraybuffer) => { let save2DiskCache = async (arraybuffer:ArrayBuffer) => {
await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer) await this.mDiskCacheProxy.putValue(this.options.generateDataKey, arraybuffer)
// 落盘之后需要主动移除当前request并且调用下一个加载 // 落盘之后需要主动移除当前request并且调用下一个加载
let removeCurrentAndSearchNextRun = this.options.removeCurrentAndSearchNext.bind(this.options) let removeCurrentAndSearchNextRun = this.options.removeCurrentAndSearchNext
removeCurrentAndSearchNextRun(); removeCurrentAndSearchNextRun();
} }
let runSave2Disk = (resolve, reject) => { let runSave2Disk = (resolve:(value:ArrayBuffer)=>void|PromiseLike<ArrayBuffer>, reject:(reason?:BusinessError|string)=>void) => {
resolve(source); resolve(source);
} }
let promise = new Promise(runSave2Disk); let promise = new Promise(runSave2Disk);
@ -533,49 +535,49 @@ export class RequestManager {
onComplete(imageKnifeData); onComplete(imageKnifeData);
} }
thumbnailProcess(source:ArrayBuffer, filetype:string, onComplete, onError){ thumbnailProcess(source:ArrayBuffer, filetype:string, onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void){
let thumbOption = new RequestOption(); let thumbOption = new RequestOption();
thumbOption.setImageViewSize({ thumbOption.setImageViewSize({
width: Math.round(this.options.thumbSizeMultiplier * this.options.size.width), width: Math.round(this.options.thumbSizeMultiplier * this.options.size.width),
height: Math.round(this.options.thumbSizeMultiplier * this.options.size.height) height: Math.round(this.options.thumbSizeMultiplier * this.options.size.height)
}) })
let thumbCallback = this.options.thumbholderOnComplete.bind(this.options); let thumbCallback = this.options.thumbholderOnComplete
let thumbError = this.options.thumbholderOnError.bind(this.options); let thumbError = this.options.thumbholderOnError
this.options.transformations[0].transform(source, thumbOption, (error, pixelMap: PixelMap) => { this.options.transformations[0].transform(source, thumbOption, {asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => {
if (pixelMap) { if (pixelMap) {
let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap); let imageKnifeData = this.createImagePixelMap(ImageKnifeType.PIXELMAP, pixelMap);
thumbCallback(imageKnifeData); thumbCallback(imageKnifeData);
} else { } else {
thumbError(error); thumbError(error);
} }
}) }})
setTimeout(() => { setTimeout(() => {
this.options.transformations[0].transform(source, this.options, (error, pixelMap: PixelMap) => { this.options.transformations[0].transform(source, this.options,{asyncTransform: (error:BusinessError|string, pixelMap: PixelMap) => {
if (pixelMap) { if (pixelMap) {
this.saveCacheAndDisk(pixelMap, filetype, onComplete, source); this.saveCacheAndDisk(pixelMap, filetype, onComplete, source);
} else { } else {
onError(error); onError(error);
} }
}) }})
}, this.options.thumbDelayTime) }, this.options.thumbDelayTime)
} }
private svgProcess(onComplete, onError, arraybuffer, typeValue, cacheStrategy?: (cacheData: ImageKnifeData) => void) { private svgProcess(onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void, arraybuffer:ArrayBuffer, typeValue:string, cacheStrategy?: (cacheData: ImageKnifeData) => void) {
let svgParseImpl = new SVGParseImpl() let svgParseImpl = new SVGParseImpl()
let size = { width: this.options.size.width, height: this.options.size.height } let size:Size = { width: this.options.size.width, height: this.options.size.height }
svgParseImpl.parseSvg(arraybuffer, size).then((value: PixelMap) => { svgParseImpl.parseSvg(arraybuffer, size).then((value: PixelMap) => {
let imageKnifeData = ImageKnifeData.createImagePixelMap(ImageKnifeType.PIXELMAP, value) let imageKnifeData = ImageKnifeData.createImagePixelMap(ImageKnifeType.PIXELMAP, value)
if(cacheStrategy){ if(cacheStrategy){
cacheStrategy(imageKnifeData) cacheStrategy(imageKnifeData)
} }
onComplete(imageKnifeData) onComplete(imageKnifeData)
}).catch(err => { }).catch((err:BusinessError) => {
onError(err) onError(err)
}) })
} }
private gifProcess(onComplete, onError, arraybuffer, typeValue, cacheStrategy?: (cacheData: ImageKnifeData) => void) { private gifProcess(onComplete:(value:ImageKnifeData)=>void|PromiseLike<ImageKnifeData>, onError:(reason?:BusinessError|string)=>void, arraybuffer:ArrayBuffer, typeValue:string, cacheStrategy?: (cacheData: ImageKnifeData) => void) {
let gifParseImpl = new GIFParseImpl() let gifParseImpl = new GIFParseImpl()
gifParseImpl.parseGifs(arraybuffer, (data?,err?)=>{ gifParseImpl.parseGifs(arraybuffer, (data?:GIFFrame[],err?:BusinessError|string)=>{
if(err){ if(err){
onError(err) onError(err)
} }

View File

@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { BusinessError } from '@ohos.base'
export interface AsyncTransform<T> { export interface AsyncTransform<T> {
(err, data: T) asyncTransform:(err:BusinessError|string, data: T)=>void
} }

View File

@ -19,9 +19,11 @@ import { parseBufferToFrame } from './parse/GIFParse'
import { LogUtil } from '../../utils/LogUtil' import { LogUtil } from '../../utils/LogUtil'
import image from '@ohos.multimedia.image' import image from '@ohos.multimedia.image'
import { ImageKnifeGlobal } from '../../ImageKnifeGlobal' import { ImageKnifeGlobal } from '../../ImageKnifeGlobal'
import { BusinessError } from '@ohos.base'
import worker from '@ohos.worker';
export class GIFParseImpl implements IParseGif { export class GIFParseImpl implements IParseGif {
parseGifs(imageinfo: ArrayBuffer, callback: (data?, err?) => void, worker?,runMainThread?:boolean) { parseGifs(imageinfo: ArrayBuffer, callback: (data?:GIFFrame[], err?:BusinessError|string) => void, worker?:worker.ThreadWorker,runMainThread?:boolean) {
let resolveWorker = worker; let resolveWorker = worker;
LogUtil.log('parseGifs resolveWorker1 is null =' + (resolveWorker == null)) LogUtil.log('parseGifs resolveWorker1 is null =' + (resolveWorker == null))
if (!resolveWorker) { if (!resolveWorker) {

View File

@ -12,7 +12,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { BusinessError } from '@ohos.base'
import { GIFFrame } from './GIFFrame'
import worker from '@ohos.worker';
export interface IParseGif{ export interface IParseGif{
// gif解析 // gif解析
parseGifs(imageinfo:ArrayBuffer,callback:(data?,err?)=>void,worker?) parseGifs:(imageinfo: ArrayBuffer, callback: (data?:GIFFrame[], err?:BusinessError|string) => void, worker?:worker.ThreadWorker,runMainThread?:boolean)=>void
} }

View File

@ -12,11 +12,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import type {IParseSvg} from'./IParseSvg' import {IParseSvg} from'./IParseSvg'
import {SVGImageView} from '@ohos/svg' import {SVGImageView} from '@ohos/svg'
export interface Size{ import {Size} from '../../RequestOption'
width:number,height:number
}
export class SVGParseImpl implements IParseSvg{ export class SVGParseImpl implements IParseSvg{
parseSvg(imageinfo: ArrayBuffer,size?:Size): Promise<PixelMap>{ parseSvg(imageinfo: ArrayBuffer,size?:Size): Promise<PixelMap>{
let model = new SVGImageView.SVGImageViewModel(); let model = new SVGImageView.SVGImageViewModel();