ImageKnife/entry/src/main/ets/pages/testImageKnifeHttpRequestHe...

173 lines
5.8 KiB
Plaintext

/*
* Copyright (C) 2022 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 {
HeaderOptions,ImageKnife,ImageKnifeComponent,ImageKnifeData,ImageKnifeGlobal,RequestOption,ImageKnifeOption,ObjectKey
} from '@ohos/libraryimageknife'
import image from '@ohos.multimedia.image'
import { BusinessError } from '@ohos.base'
const TAG = "TEST-"
let timeId = -1
@Entry
@Component
struct testImageKnifeHttpRequestHeader1 {
@State pixelMap: PixelMap | undefined = undefined;
@State pixelMap1: PixelMap | undefined = undefined;
@State domeType1: boolean = false;
@State domeType2: boolean = false;
@State successHeader: string = "requestOption调用成功";
@State errorHeader: string = "requestOption调用失败";
@State allKeySame: string = "全局添加header和request成功的键相同值不同";
@State allKeyNoSame: string = "全局添加header和request成功的键不同";
@State clearnAllHeader: string = "清空全局header";
@State message: string = "图片header属性测试";
imageKnife: ImageKnife | undefined = ImageKnifeGlobal.getInstance().getImageKnife()
// RequestOption调用
load(src: string | image.PixelMap | Resource, type: string, num: number) {
clearTimeout(timeId)
let request = new RequestOption()
if (type == 'error') {
request.addHeader('xx', src)
} else {
request.addHeader('refer', src)
}
//清理缓存
request.signature = new ObjectKey(new Date().getTime().toString())
request.load(src)
.addListener({ callback: (err: BusinessError | string, data: ImageKnifeData) => {
if (data.isPixelMap()) {
if (data.drawPixelMap) {
let pixelmap = data.drawPixelMap.imagePixelMap
if (pixelmap) {
if (num == 1) {
this.pixelMap = pixelmap
} else if (num == 2) {
this.pixelMap1 = pixelmap
}
}
}
}
if (data.isGIFFrame()) {
let index: number = 0
if (data.drawGIFFrame) {
if (data.drawGIFFrame.imageGIFFrames) {
let renderGif = () => {
if (data.drawGIFFrame) {
if (data.drawGIFFrame.imageGIFFrames) {
let pixelmap = data.drawGIFFrame.imageGIFFrames[index].drawPixelMap
let delay = data.drawGIFFrame.imageGIFFrames[index].delay
if (pixelmap) {
if (num == 1) {
this.pixelMap = pixelmap
} else if (num == 2) {
this.pixelMap1 = pixelmap
}
}
index++;
if (index == data.drawGIFFrame.imageGIFFrames.length - 1) {
index = 0
}
timeId = setTimeout(renderGif, data!.drawGIFFrame!.imageGIFFrames![index].delay)
}
}
}
renderGif()
}
}
}
if (err) {
console.log(TAG + "error:" + JSON.stringify(err));
}
return false
}
})
let imageknife: ImageKnife | undefined = ImageKnifeGlobal.getInstance().getImageKnife()
if (imageknife != undefined) {
imageknife.call(request)
}
}
@Builder
setHeader() {
ImageKnifeComponent({
imageKnifeOption: {
loadSrc: this.pixelMap as image.PixelMap
}
}).width(200).height(200).borderWidth(1)
}
@Builder
setHeader1() {
ImageKnifeComponent({
imageKnifeOption: {
loadSrc: this.pixelMap1 as image.PixelMap
}
}).width(200).height(200).borderWidth(1)
}
build() {
Scroll() {
Column() {
Text(`${this.message}`)
.width("300vp")
.height("60vp")
.textAlign(TextAlign.Center)
.fontSize("30fp")
.fontWeight(FontWeight.Bold)
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Start }) {
Button(this.successHeader)
.margin(16)
.onClick(() => {
this.domeType1 = !this.domeType1
this.load('http://1.94.37.200:7070/AntiTheftChain/downloadImage', 'success', 1)
})
if (this.domeType1) {
this.setHeader()
}
Button(this.errorHeader)
.margin(16)
.onClick(() => {
this.domeType2 = !this.domeType2
this.load('http://1.94.37.200:7070/AntiTheftChain/downloadImage', 'error', 2)
})
if (this.domeType2) {
this.setHeader1()
}
Button(this.allKeySame)
.margin(16)
.onClick(() => {
this.imageKnife?.addHeader('refer','test')
})
Button(this.allKeyNoSame)
.margin(16)
.onClick(() => {
this.imageKnife?.addHeader('ceshi','http://1.94.37.200:7070/AntiTheftChain/downloadImage')
})
Button(this.clearnAllHeader)
.margin(16)
.onClick(() => {
this.imageKnife?.deleteHeader('refer');
this.imageKnife?.deleteHeader('ceshi');
})
}
}.width("100%")
.justifyContent(FlexAlign.Start)
}
}
}