forked from floraachy/ImageKnife
!200 修改全局请求头覆盖request请求头
Merge pull request !200 from taxuexunji/master
This commit is contained in:
commit
90ff9edf1d
|
@ -6,6 +6,7 @@
|
|||
- 修改磁盘缓存到子线程
|
||||
- 更新SDK到API12
|
||||
- 适配Sendable内存共享优化
|
||||
- 修改全局请求头覆盖request请求头
|
||||
|
||||
## 2.1.2-rc.12
|
||||
- 新增gif播放次数功能
|
||||
|
|
|
@ -105,6 +105,9 @@ imageKnifeOption = {
|
|||
|
||||
代码示例
|
||||
|
||||
### 6.自定义请求头规格
|
||||
设置全局header并且设置request的header时,当key不同时全局和request并行,当key相同时request的header覆盖全局的header
|
||||
|
||||
## 进阶使用
|
||||
|
||||
如果简单的加载一张图像无法满足需求,我们可以看看ImageKnifeOption这个类提供了哪些扩展能力。
|
||||
|
|
|
@ -367,6 +367,10 @@ struct IndexFunctionDemo {
|
|||
.onClick(() => {
|
||||
router.pushUrl({ url: 'pages/testImageKnifeDataFetch' });
|
||||
}).margin({ top: 5, left: 3 })
|
||||
Button('全局header和request的header')
|
||||
.onClick(() => {
|
||||
router.pushUrl({ url: 'pages/testImageKnifeHttpRequestHeader1' });
|
||||
}).margin({ top: 5, left: 3 })
|
||||
}.width('100%').height(60).backgroundColor(Color.Pink)
|
||||
|
||||
Text('测试图片缓存内存').fontSize(15)
|
||||
|
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -43,6 +43,7 @@
|
|||
"pages/testImageKnifeRouter2",
|
||||
"pages/RequestOptionLoadImage",
|
||||
"pages/testImageKnifeHttpRequestHeader",
|
||||
"pages/testImageKnifeHttpRequestHeader1",
|
||||
"pages/testImageKnifeAutoPage",
|
||||
"pages/testImageKnifeAutoWidthPage",
|
||||
"pages/testImageKnifeAutoHeightPage",
|
||||
|
|
|
@ -93,7 +93,11 @@ export class RequestOption {
|
|||
|
||||
// 全局调用header对应的方法,包含RequestOption的形式
|
||||
addHeaderMap(map: Map<string, Object>) {
|
||||
this.headers = map;
|
||||
map.forEach((value, key) => {
|
||||
if (!this.headers.has(key)) {
|
||||
this.addHeader(key, value);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 优先级
|
||||
|
|
Loading…
Reference in New Issue