1.ArkTs整改11 整改imageknife->resourcemanager

Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
zhoulisheng1 2023-09-19 16:51:01 +08:00
parent 76ab03262f
commit 3595190083
3 changed files with 19 additions and 20 deletions

View File

@ -13,9 +13,8 @@
* limitations under the License. * limitations under the License.
*/ */
import {RequestOption} from "../RequestOption" import { BusinessError } from '@ohos.base'
// 本地资源解析抽象接口 // 本地资源解析抽象接口
export interface IResourceFetch { export interface IResourceFetch<T> {
loadResource(res: Resource, onCompleteFunction, onErrorFunction); loadResource:(res: Resource, onCompleteFunction:(value:T)=>void | PromiseLike<T>, onErrorFunction:(reason?:BusinessError|string)=>void)=>void;
} }

View File

@ -13,14 +13,13 @@
* limitations under the License. * limitations under the License.
*/ */
import type {IResourceFetch} from '../resourcemanage/IResourceFetch' import {IResourceFetch} from '../resourcemanage/IResourceFetch'
import {ResourceTypeEts} from '../../imageknife/constants/ResourceTypeEts' import {ResourceTypeEts} from '../../imageknife/constants/ResourceTypeEts'
import resourceManager from '@ohos.resourceManager'; import resourceManager from '@ohos.resourceManager';
import { ImageKnifeGlobal } from '../ImageKnifeGlobal'; import { ImageKnifeGlobal } from '../ImageKnifeGlobal';
import { BusinessError } from '@ohos.base'
export class ParseResClient implements IResourceFetch { export class ParseResClient implements IResourceFetch<ArrayBuffer> {
loadResource(res: Resource, onCompleteFunction, onErrorFunction) { loadResource(res: Resource, onCompleteFunction:(value:ArrayBuffer)=>void | PromiseLike<ArrayBuffer>, onErrorFunction:(reason?:BusinessError|string)=>void) {
let resId = res.id; let resId = res.id;
let resType = res.type; let resType = res.type;
if (resType == ResourceTypeEts.MEDIA) { if (resType == ResourceTypeEts.MEDIA) {
@ -30,7 +29,7 @@ export class ParseResClient implements IResourceFetch {
let arrayBuffer = this.typedArrayToBuffer(data); let arrayBuffer = this.typedArrayToBuffer(data);
onCompleteFunction(arrayBuffer) onCompleteFunction(arrayBuffer)
}) })
.catch(err => { .catch( (err:BusinessError) => {
onErrorFunction(err) onErrorFunction(err)
}) })
} }

View File

@ -13,31 +13,32 @@
* limitations under the License. * limitations under the License.
*/ */
import type {IResourceFetch} from '../resourcemanage/IResourceFetch' import { IResourceFetch } from '../resourcemanage/IResourceFetch'
import {ResourceTypeEts} from '../../imageknife/constants/ResourceTypeEts' import { ResourceTypeEts } from '../../imageknife/constants/ResourceTypeEts'
import {Base64} from '../../cache/Base64' import { Base64 } from '../../cache/Base64'
import { BusinessError } from '@ohos.base'
import resourceManager from '@ohos.resourceManager'; import resourceManager from '@ohos.resourceManager';
import { ImageKnifeGlobal } from '../ImageKnifeGlobal'; import { ImageKnifeGlobal } from '../ImageKnifeGlobal';
export class ParseResClientBase64 implements IResourceFetch { export class ParseResClientBase64 implements IResourceFetch<ArrayBuffer> {
loadResource(res: Resource, onCompleteFunction, onErrorFunction) { loadResource(res: Resource, onCompleteFunction: (value: ArrayBuffer) => void | PromiseLike<ArrayBuffer>, onErrorFunction: (reason?: BusinessError | string) => void) {
let resId = res.id; let resId = res.id;
let resType = res.type; let resType = res.type;
if (resType == ResourceTypeEts.MEDIA) { if (resType == ResourceTypeEts.MEDIA) {
((ImageKnifeGlobal.getInstance().getHapContext() as Record<string,Object>).resourceManager as resourceManager.ResourceManager) ((ImageKnifeGlobal.getInstance()
.getHapContext() as Record<string, Object>).resourceManager as resourceManager.ResourceManager)
.getMediaContentBase64(resId) .getMediaContentBase64(resId)
.then(data => { .then(data => {
let matchReg = ';base64,'; let matchReg = ';base64,';
var firstIndex = data.indexOf(matchReg) let firstIndex = data.indexOf(matchReg)
data = data.substring(firstIndex + matchReg.length, data.length) data = data.substring(firstIndex + matchReg.length, data.length)
let arrayBuffer = Base64.getInstance() let arrayBuffer = Base64.getInstance()
.decode(data); .decode(data);
onCompleteFunction(arrayBuffer) onCompleteFunction(arrayBuffer)
}) })
.catch(err => { .catch((err: BusinessError) => {
onErrorFunction(err) onErrorFunction(err)
}) })
} }
else if (resType == ResourceTypeEts.RAWFILE) { else if (resType == ResourceTypeEts.RAWFILE) {
onErrorFunction('ParseResClientBase64 本地资源是rawfile暂时无法解析出错') onErrorFunction('ParseResClientBase64 本地资源是rawfile暂时无法解析出错')