Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
006d5bad
D
Docs
项目概览
OpenHarmony
/
Docs
大约 2 年 前同步成功
通知
161
Star
293
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
006d5bad
编写于
5月 09, 2023
作者:
D
duangavin123
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改案例
Signed-off-by:
N
duangavin123
<
duanxichao@huawei.com
>
上级
56893c15
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
190 addition
and
0 deletion
+190
-0
zh-cn/third-party-cases/figures/httpforimage.gif
zh-cn/third-party-cases/figures/httpforimage.gif
+0
-0
zh-cn/third-party-cases/how-to-load-images-from-internet.md
zh-cn/third-party-cases/how-to-load-images-from-internet.md
+190
-0
未找到文件。
zh-cn/third-party-cases/figures/httpforimage.gif
0 → 100644
浏览文件 @
006d5bad
49.1 KB
zh-cn/third-party-cases/how-to-load-images-from-internet.md
0 → 100644
浏览文件 @
006d5bad
# 如何请求并加载网络图片
## 场景说明
加载网络图片时,如果直接在Image组件中写入图片的url链接,当网速较慢或者图片较大时,可能导致Image组件加载不出图片。因此,建议使用http接口请求图片数据,等数据返回后通过状态变量实时将图片加载到页面上,这样就能保证图片的渲染。本例将为大家介绍如何实现上述场景。
## 效果呈现
本例效果如下:

## 环境要求
-
IDE:DevEco Studio 3.1 Beta1
-
SDK:Ohos_sdk_public 3.2.11.9 (API Version 9 Release)
## 实现思路
-
使用http请求获取图片数据。
-
通过image.createImageSource()将获取到的数据转化为图片源实例。
-
调用createPixelMap()将图片数据解析为pixelmap。
-
将pixelmap通过状态变量传入Image组件进行渲染显示。
## 开发步骤
1.
申请网络权限。
使用http请求需要先申请网络权限,在module.json5文件中添加以下配置:
```
json
"requestPermissions"
:[
{
"name"
:
"ohos.permission.INTERNET"
,
},
]
```
2.
构建UI框架。
```ts
@Entry
@Component
struct NetPic {
// 先创建一个PixelMap状态变量用于接收网络图片数据
@State image: PixelMap = undefined
build() {
Column({ space: 10 }) {
Button("获取网络图片")
.onClick(() => {
this.httpRequest();
})
Image().height(100).width(100)
}
.width('100%')
.height('100%')
.padding(10)
}
}
```
3.
先使用http请求获取图片数据,再通过image.createImageSource()将获取到的数据转化为图片源实例,然后调用createPixelMap()将图片数据解析为pixelmap。
```
ts
@
Entry
@
Component
struct
NetPic
{
// 先创建一个PixelMap状态变量用于接收网络图片
@
State
image
:
PixelMap
=
undefined
// 网络图片请求方法
httpRequest
()
{
let
httpRequest
=
http
.
createHttp
();
httpRequest
.
request
(
"
https://images.openharmony.cn/330-%E8%BF%90%E8%90%…%BE/%E6%B4%BB%E5%8A%A8/419/3.2releas-1920-480.jpg
"
,
//网络图片地址
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
error code:
"
+
error
.
code
+
"
, msg:
"
+
error
.
message
)
}
else
{
let
code
=
data
.
responseCode
if
(
ResponseCode
.
ResponseCode
.
OK
==
code
)
{
let
res
:
any
=
data
.
result
//通过获取到的uri创建图片源实例。
let
imageSource
=
image
.
createImageSource
(
res
)
let
options
=
{
// 透明度
alphaType
:
0
,
// 是否可编辑
editable
:
false
,
// 像素格式
pixelFormat
:
3
,
// 缩略值
scaleMode
:
1
,
// 创建图片大小
size
:
{
height
:
100
,
width
:
100
}
}
//将图片数据解析为pixelmap,并设置option属性。
imageSource
.
createPixelMap
(
options
).
then
((
pixelMap
)
=>
{
// 将pixelmap通过状态变量image传递给Image组件,将图片信息加载出来。
this
.
image
=
pixelMap
})
}
else
{
console
.
log
(
"
response code:
"
+
code
);
}
}
}
)
}
...
}
```
4.
将状态变量image传递给Image组件,将图片加载出来。
```
ts
...
build
()
{
Column
({
space
:
10
})
{
Button
(
"
获取网络图片
"
)
.
onClick
(()
=>
{
this
.
httpRequest
();
})
// 将状态变量image传递给Image组件,当image获取到图片数据时,可以实时刷新UI
Image
(
this
.
image
).
height
(
100
).
width
(
100
)
}
.
width
(
'
100%
'
)
.
height
(
'
100%
'
)
.
padding
(
10
)
}
...
```
## 完整代码
本例完整代码如下:
```
ts
import
http
from
'
@ohos.net.http
'
;
import
ResponseCode
from
'
@ohos.net.http
'
;
import
image
from
'
@ohos.multimedia.image
'
@
Entry
@
Component
struct
NetPic
{
// 先创建一个PixelMap状态变量用于接收网络图片
@
State
image
:
PixelMap
=
undefined
// 网络图片请求方法
httpRequest
()
{
let
httpRequest
=
http
.
createHttp
();
httpRequest
.
request
(
"
https://images.openharmony.cn/330-%E8%BF%90%E8%90%…%BE/%E6%B4%BB%E5%8A%A8/419/3.2releas-1920-480.jpg
"
,
//网络图片地址
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
error code:
"
+
error
.
code
+
"
, msg:
"
+
error
.
message
)
}
else
{
let
code
=
data
.
responseCode
if
(
ResponseCode
.
ResponseCode
.
OK
==
code
)
{
let
res
:
any
=
data
.
result
//通过获取到的uri创建图片源实例。
let
imageSource
=
image
.
createImageSource
(
res
)
let
options
=
{
// 透明度
alphaType
:
0
,
// 是否可编辑
editable
:
false
,
// 像素格式
pixelFormat
:
3
,
// 缩略值
scaleMode
:
1
,
// 创建图片大小
size
:
{
height
:
100
,
width
:
100
}
}
//将图片数据解析为pixelmap,并设置option属性。
imageSource
.
createPixelMap
(
options
).
then
((
pixelMap
)
=>
{
// 将pixelmap通过状态变量image传递给Image组件,将图片信息加载出来。
this
.
image
=
pixelMap
})
}
else
{
console
.
log
(
"
response code:
"
+
code
);
}
}
}
)
}
build
()
{
Column
({
space
:
10
})
{
Button
(
"
获取网络图片
"
)
.
onClick
(()
=>
{
this
.
httpRequest
();
})
// 将状态变量image传递给Image组件,当image获取到图片数据时,可以实时刷新UI
Image
(
this
.
image
).
height
(
100
).
width
(
100
)
}
.
width
(
'
100%
'
)
.
height
(
'
100%
'
)
.
padding
(
10
)
}
}
```
## 参考
[
图片处理
](
../application-dev/reference/apis/js-apis-image.md
)
[
数据请求
](
../application-dev/reference/apis/js-apis-http.md
)
[
权限申请指导
](
../application-dev/security/accesstoken-guidelines.md
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录