当前位置: 首页 > news >正文

鸿蒙加载网络图片并转换成PixelMap

鸿蒙加载网络图片并转换成PixelMap

参考文档

基于API12.

有一些图片功能需要使用 PixelMap 类型的参数,但是使用Image组件之类的时候无法获取到 PixelMap 类型数据。
因此只能是把图片下载下来然后加在并转换一下。

实现方式

一下封装了一个函数。使用的 rcp 模块,现在所有的请求都推荐使用这个网络请求的模块。之后就能方便的获取到 PixelMap 类型的数据了。而且这个类型的数据还能直接给 Image 组件使用。

requestImageUrl(url: string) : Promise<image.PixelMap> {return new Promise<image.PixelMap>((resolve, reject) => {rcp.createSession().get(url).then((response) => {console.info(`Succeeded in getting the response ${response}`);let imgData: ArrayBuffer = response.body as ArrayBufferconsole.info(`request image success, size: ${imgData.byteLength}`);let imgSource: image.ImageSource = image.createImageSource(imgData);imgSource.createPixelMap().then((pixelMap: PixelMap) => {console.error('image createPixelMap success');resolve(pixelMap)}).catch((err: BusinessError) => {console.error(`err: err code is ${err.code}, err message is ${JSON.stringify(err)}`);reject(err)});}).catch((err: BusinessError) => {console.error(`err: err code is ${err.code}, err message is ${JSON.stringify(err)}`);reject(err)});})
}

使用方式

使用方式如下:

let img = await this.requestImageUrl('http://gips2.baidu.com/it/u=3944689179,983354166&fm=3028&app=3028&f=JPEG&fmt=auto?w=1024&h=1024')// build 里边
Image(img)

结果如下:

http://www.lryc.cn/news/486345.html

相关文章:

  • hive搭建
  • 51c扩散模型~合集1
  • 从零开始深度学习:全连接层、损失函数与梯度下降的详尽指南
  • Liebherr利勃海尔 EDI 需求分析
  • java小练习
  • go语言中的占位符有哪些
  • 基于Windows安装opus python库
  • 【设计模式】行为型模式(五):解释器模式、访问者模式、依赖注入
  • 使用nossl模式连接MySQL数据库详解
  • 【MySQL】ubantu 系统 MySQL的安装与免密码登录的配置
  • 高级 SQL 技巧讲解
  • 浅论AI大模型在电商行业的发展未来
  • 【python笔记03】《类》
  • Flutter 应用在真机上调试的流程
  • 以太坊基础知识结构详解
  • 安全见闻(完整版)
  • LeetCode100之反转链表(206)--Java
  • 牛客周赛第一题2024/11/17日
  • 麒麟Server下安装东方通TongLINK/Q
  • BERT的中文问答系统33
  • Ubuntu下的Eigen库的安装及基本使用教程
  • 【spring 】Spring Cloud Gateway 的Filter学习
  • 每秒交易数(Transactions Per Second:TPS)详细拆解
  • 【初阶数据结构与算法】链表刷题之链表分割、相交链表、环形链表1、环形链表I、环形链表II
  • 【STL】set,multiset,map,multimap的介绍以及使用
  • 新能源二手车交易量有望破百万,二手车市场回暖了吗?
  • 哈佛商业评论 | 项目经济的到来:组织变革与管理革新的关键
  • web浏览器环境下使用window.open()打开PDF文件不是预览,而是下载文件?
  • 【GeekBand】C++设计模式笔记12_Singleton_单件模式
  • Pyhon基础数据结构(列表)【蓝桥杯】