HarmonyOS 通过传入的uri创建图片源实例?

返回的网络图片链接,怎么创建图片实例

阅读 440
1 个回答
import { http } from '@kit.NetworkKit'
import ResponseCode from '@ohos.net.http';
import image from '@ohos.multimedia.image';


loadImageWithUrl() {
  // 使用request下载图片
  http.createHttp().request("http:www.tupian.com",
    {
      method:http.RequestMethod.GET,
      connectTimeout:60000,
      readTimeout:60000
    },
    async (error: BusinessError, data: http.HttpResponse) => {
      if (error) {
        console.error(`http reqeust failed with. Code: ${error.code}, message: ${error.message}`);
      } else {
        if (ResponseCode.ResponseCode.OK === data.responseCode) {
          let imageBuffer: ArrayBuffer = data.result as ArrayBuffer;
          //创建图片源实例
          const imageSourceApi: image.ImageSource = image.createImageSource(imageBuffer);
          console.log("图片下载成功")

        } else {
          console.error("error occurred when image downloaded!")
        }
      }
    })
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进