Q:如何使用Image组件加载本地图片?
Image('data:image/jpg;base64,' + this.base64ImageData) Q:Image组件如何加载apng图片?
A:可以通过ohos_apng库来实现。
Q:在开发过程中会遇到很多加载网络图片的场景,例如使用Web组件加载网络图片,使用Image组件加载网络图片。如何使用这两个组件加载网络图片以及两个组件的区别和适用场景是什么?
A:方案一:使用Web组件进行图片加载。
使用Web组件加载网络图片资源时,需添加网络权限:ohos.permission.INTERNET,具体申请方式请参考声明权限。使用Web组件加载网络图片,可以直接加载图片的在线地址或者带后缀名的图片地址,其具备两个属性:onlineImageAccess和imageAccess,onlineImageAccess设置是否允许从网络加载图片资源(通过HTTP和HTTPS访问的资源),默认允许访问,默认值:true;imageAccess设置是否允许自动加载图片资源,默认允许,默认值:true。代码示例如下:
Web({ src: $rawfile('index.html'), controller: this.controller })
.height(200)
.javaScriptAccess(true)
.onlineImageAccess(true)
.imageAccess(true)
.fileAccess(true)
.geolocationAccess(false)
.domStorageAccess(true); 方案二:使用Image组件加载网络图片。
使用Image组件加载网络图片时,默认网络超时是5分钟,在加载网络图片的过程中,建议使用alt配置加载时的占位图,使用HTTP工具包发送网络请求,然后将返回的数据解码为Image组件中的PixelMap,图片开发可参考图片处理。使用Image加载网络图片时,需要申请权限ohos.permission.INTERNET,具体申请方式请参考声明权限。
Image(this.url)
.height(150)
.objectFit(ImageFit.Auto)
.onError((err: ImageError) => {
// 图片无法加载时触发
console.error(`Failed to do sth. Code: ${err.error?.code}, message: ${err.message}`);
})
.alt($r('app.media.startIcon')); // 通过http的request方法从网络下载图片资源
async getPicture() {
http.createHttp()
.request(this.url, (error: BusinessError, data: http.HttpResponse) => {
if (error) {
// 下载失败时弹窗提示检查网络,不执行后续逻辑
console.error(`request failed, ${JSON.stringify(error.message)}`);
return;
}
this.transcodePixelMap(data);
});
} /**
* 使用createPixelMap将ArrayBuffer类型的图片转换为PixelMap类型
* @param data:网络获取到的资源
*/
transcodePixelMap(data: http.HttpResponse) {
if (http.ResponseCode.OK === data.responseCode) {
const imageData: ArrayBuffer = data.result as ArrayBuffer;
// 通过ArrayBuffer创建图片源实例
const imageSource: image.ImageSource = image.createImageSource(imageData);
let imageInfo = imageSource.getImageInfoSync(0);
let imgWidth = imageInfo.size.width;
let imgHeight = imageInfo.size.height;
const options: image.DecodingOptions = {
'editable': false,
'desiredSize': { width: imgWidth, height: imgHeight }
};
// 通过属性创建PixelMap
imageSource.createPixelMap(options).then((pixelMap: PixelMap) => {
this.image = pixelMap;
});
}
} Image(this.image!)
.width('100%')
.objectFit(ImageFit.Auto); 完整代码如下:
import { webview } from '@kit.ArkWeb';
import { http } from '@kit.NetworkKit';
import { image } from '@kit.ImageKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
controller: WebviewController = new webview.WebviewController();
uiContext: UIContext = this.getUIContext();
@State image: PixelMap | null = null;
url: string =
encodeURI(''); // 网络图片链接url
aboutToAppear() {
this.getPicture();
webview.WebviewController.setWebDebuggingAccess(true);
}
// 通过http的request方法从网络下载图片资源
async getPicture() {
http.createHttp()
.request(this.url, (error: BusinessError, data: http.HttpResponse) => {
if (error) {
// 下载失败时弹窗提示检查网络,不执行后续逻辑
console.error(`request failed, ${JSON.stringify(error.message)}`);
return;
}
this.transcodePixelMap(data);
});
}
/**
* 使用createPixelMap将ArrayBuffer类型的图片转换为PixelMap类型
* @param data:网络获取到的资源
*/
transcodePixelMap(data: http.HttpResponse) {
if (http.ResponseCode.OK === data.responseCode) {
const imageData: ArrayBuffer = data.result as ArrayBuffer;
// 通过ArrayBuffer创建图片源实例
const imageSource: image.ImageSource = image.createImageSource(imageData);
let imageInfo = imageSource.getImageInfoSync(0);
let imgWidth = imageInfo.size.width;
let imgHeight = imageInfo.size.height;
const options: image.DecodingOptions = {
'editable': false,
'desiredSize': { width: imgWidth, height: imgHeight }
};
// 通过属性创建PixelMap
imageSource.createPixelMap(options).then((pixelMap: PixelMap) => {
this.image = pixelMap;
});
}
}
build() {
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
// 方案一:使用Web组件进行图片加载
Web({ src: $rawfile('index.html'), controller: this.controller })
.height(200)
.javaScriptAccess(true)
.onlineImageAccess(true)
.imageAccess(true)
.fileAccess(true)
.geolocationAccess(false)
.domStorageAccess(true);
// 方案二:使用Image组件加载网络图片
// 直接加载
Image(this.url)
.height(150)
.objectFit(ImageFit.Auto)
.onError((err: ImageError) => {
// 图片无法加载时触发
console.error(`Failed to do sth. Code: ${err.error?.code}, message: ${err.message}`);
})
.alt($r('app.media.startIcon'));
// 解码后加载
Image(this.image!)
.width('100%')
.objectFit(ImageFit.Auto);
}
.height('100%')
.width('100%');
}
} <!-- main/resources/resfile/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, viewport-fit=cover">
<script>
</script>
</head>
<body>
<div class="page">
<!-- 网络图片链接url-->
<img src="" width="250" height="auto">
</div>
</body>
</html> Q:使用Web组件进行图片加载和使用Image组件进行图片加载有什么区别?
A:使用Web组件和Image组件都支持加载本地图片和网络图片,但对于PixelMap类型图片Image组件可以直接加载,Web组件需要将PixelMap转化为base64字符串,或将其保存为本地图片并获取本地图片路径后用于加载。
Q:需要加载的图片文件名或路径中含有空格、特殊字符或者用户在填写表单时输入了一个带有空格的图片链接,或者某个API返回的图片地址中包含了未经编码的特殊字符,如何使用Image组件进行加载?
A:可以使用encodeURI编码对包含空格或特殊字符的图片地址进行编码,将这些字符替换为对应的编码形式,然后使用Image组件加载。代码示例如下:
import { webview } from '@kit.ArkWeb';
import { http } from '@kit.NetworkKit';
import { image } from '@kit.ImageKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
controller: WebviewController = new webview.WebviewController();
uiContext: UIContext = this.getUIContext();
@State image: PixelMap | null = null;
url: string =
encodeURI(''); // 网络图片链接url
aboutToAppear() {
this.getPicture();
webview.WebviewController.setWebDebuggingAccess(true);
}
// 通过http的request方法从网络下载图片资源
async getPicture() {
http.createHttp()
.request(this.url, (error: BusinessError, data: http.HttpResponse) => {
if (error) {
// 下载失败时弹窗提示检查网络,不执行后续逻辑
console.error(`request failed, ${JSON.stringify(error.message)}`);
return;
}
this.transcodePixelMap(data);
});
}
/**
* 使用createPixelMap将ArrayBuffer类型的图片转换为PixelMap类型
* @param data:网络获取到的资源
*/
transcodePixelMap(data: http.HttpResponse) {
if (http.ResponseCode.OK === data.responseCode) {
const imageData: ArrayBuffer = data.result as ArrayBuffer;
// 通过ArrayBuffer创建图片源实例
const imageSource: image.ImageSource = image.createImageSource(imageData);
let imageInfo = imageSource.getImageInfoSync(0);
let imgWidth = imageInfo.size.width;
let imgHeight = imageInfo.size.height;
const options: image.DecodingOptions = {
'editable': false,
'desiredSize': { width: imgWidth, height: imgHeight }
};
// 通过属性创建PixelMap
imageSource.createPixelMap(options).then((pixelMap: PixelMap) => {
this.image = pixelMap;
});
}
}
build() {
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
// 方案一:使用Web组件进行图片加载
Web({ src: $rawfile('index.html'), controller: this.controller })
.height(200)
.javaScriptAccess(true)
.onlineImageAccess(true)
.imageAccess(true)
.fileAccess(true)
.geolocationAccess(false)
.domStorageAccess(true);
// 方案二:使用Image组件加载网络图片
// 直接加载
Image(this.url)
.height(150)
.objectFit(ImageFit.Auto)
.onError((err: ImageError) => {
// 图片无法加载时触发
console.error(`Failed to do sth. Code: ${err.error?.code}, message: ${err.message}`);
})
.alt($r('app.media.startIcon'));
// 解码后加载
Image(this.image!)
.width('100%')
.objectFit(ImageFit.Auto);
}
.height('100%')
.width('100%');
}
} Q:使用Image组件加载图片时,图片无法加载,如何检测图片加载失败的原因?
A:检查网络连接和网络权限配置,并在Image组件下添加监听代码,监听图片加载失败原因代码示例如下:
import { webview } from '@kit.ArkWeb';
import { http } from '@kit.NetworkKit';
import { image } from '@kit.ImageKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
controller: WebviewController = new webview.WebviewController();
uiContext: UIContext = this.getUIContext();
@State image: PixelMap | null = null;
url: string =
encodeURI(''); // 网络图片链接url
aboutToAppear() {
this.getPicture();
webview.WebviewController.setWebDebuggingAccess(true);
}
// 通过http的request方法从网络下载图片资源
async getPicture() {
http.createHttp()
.request(this.url, (error: BusinessError, data: http.HttpResponse) => {
if (error) {
// 下载失败时弹窗提示检查网络,不执行后续逻辑
console.error(`request failed, ${JSON.stringify(error.message)}`);
return;
}
this.transcodePixelMap(data);
});
}
/**
* 使用createPixelMap将ArrayBuffer类型的图片转换为PixelMap类型
* @param data:网络获取到的资源
*/
transcodePixelMap(data: http.HttpResponse) {
if (http.ResponseCode.OK === data.responseCode) {
const imageData: ArrayBuffer = data.result as ArrayBuffer;
// 通过ArrayBuffer创建图片源实例
const imageSource: image.ImageSource = image.createImageSource(imageData);
let imageInfo = imageSource.getImageInfoSync(0);
let imgWidth = imageInfo.size.width;
let imgHeight = imageInfo.size.height;
const options: image.DecodingOptions = {
'editable': false,
'desiredSize': { width: imgWidth, height: imgHeight }
};
// 通过属性创建PixelMap
imageSource.createPixelMap(options).then((pixelMap: PixelMap) => {
this.image = pixelMap;
});
}
}
build() {
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
// 方案一:使用Web组件进行图片加载
Web({ src: $rawfile('index.html'), controller: this.controller })
.height(200)
.javaScriptAccess(true)
.onlineImageAccess(true)
.imageAccess(true)
.fileAccess(true)
.geolocationAccess(false)
.domStorageAccess(true);
// 方案二:使用Image组件加载网络图片
// 直接加载
Image(this.url)
.height(150)
.objectFit(ImageFit.Auto)
.onError((err: ImageError) => {
// 图片无法加载时触发
console.error(`Failed to do sth. Code: ${err.error?.code}, message: ${err.message}`);
})
.alt($r('app.media.startIcon'));
// 解码后加载
Image(this.image!)
.width('100%')
.objectFit(ImageFit.Auto);
}
.height('100%')
.width('100%');
}
} Q:在请求网络图片的过程中,如何进行证书校验?
A:使用Global.getContext中的filesDir方法获取应用沙箱路径,将图片下载至沙箱路径下,再用Image加载图片。
Q:使用Web组件如何加载HTTPS链接中的HTTP图片?
A:将Web组件的mixedMode属性设置为MixedMode.All来允许加载HTTP和HTTPS混合内容。
Q:Image组件加载网络图片,PixelMap与直接访问url有什么区别?
A:两者都支持加载网络图片。使用PixelMap方式时,可以对图片进行更多效果设置,功能更强大。
参考链接:Image、Interface (PixelMap)。
Q:Image组件是否有缓存机制?
A:
setImageCacheCount、setImageRawDataCacheSize和setImageFileCacheSize这三个图片缓存接口灵活性不足,后续不再演进,对于复杂情况,建议使用ImageKnife。