forked from floraachy/ImageKnife
109 lines
3.5 KiB
Plaintext
109 lines
3.5 KiB
Plaintext
/*
|
|
* 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 image from '@ohos.multimedia.image'
|
|
import {
|
|
ImageKnifeData,
|
|
RequestOption,
|
|
Size,
|
|
ImageKnife,
|
|
ImageKnifeGlobal,
|
|
ImageKnifeComponent,
|
|
ObjectKey
|
|
} from '@ohos/libraryimageknife'
|
|
import { BusinessError } from '@ohos.base'
|
|
|
|
const TAG = "TEST-"
|
|
let timeId = -1
|
|
|
|
@Entry
|
|
@Component
|
|
struct RequestOptionLoadImage {
|
|
@State pixelMap: PixelMap | undefined = undefined
|
|
|
|
load(src: string | image.PixelMap | Resource) {
|
|
clearTimeout(timeId)
|
|
let request = new RequestOption()
|
|
//*requestOption调用*
|
|
request.addHeader('refer', 'http://1.94.37.200:7070/AntiTheftChain/downloadImage');
|
|
//通过时间戳去清除缓存
|
|
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) {
|
|
this.pixelMap = 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) {
|
|
this.pixelMap = 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 compSize: Size = {
|
|
width: 300,
|
|
height: 300
|
|
}
|
|
request.setImageViewSize(compSize)
|
|
let imageknife: ImageKnife | undefined = ImageKnifeGlobal.getInstance().getImageKnife()
|
|
if (imageknife != undefined) {
|
|
imageknife.call(request)
|
|
}
|
|
}
|
|
|
|
build() {
|
|
Scroll() {
|
|
Column() {
|
|
Text("RequestOption加载图片").fontSize(25)
|
|
Button("加载网络gif").onClick(() => {
|
|
this.load("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658")
|
|
})
|
|
Button("加载静态图").onClick(() => {
|
|
this.load($r('app.media.pngSample'))
|
|
})
|
|
ImageKnifeComponent({ imageKnifeOption: {
|
|
loadSrc: this.pixelMap as image.PixelMap
|
|
} }).width(300).height(300).borderWidth(3)
|
|
}
|
|
}
|
|
}
|
|
} |