浏览 SDKs · uni-app / uni-app x
SDKsuni-app

从 URL 创建图片消息

使用已上传图片的 elem 信息创建消息。

复制

createImageMessageByURL() 使用已经上传的图片信息创建消息。原图、大图和缩略图可以指向不同资源;示例仅在三者相同时复用同一个对象。

参数说明

参数类型说明
sourcePictureOpenIMPicturenull原图信息。
bigPictureOpenIMPicturenull大图信息。
snapshotPictureOpenIMPicturenull缩略图信息。
sourcePathstringnull原始文件的本地名称或业务路径;只有远端资源时传空字符串。

三个图片对象使用相同字段:uuidtypesizewidthheighturl,均为可选值;创建完整可展示消息时应填写真实上传结果。

import { createImageMessageByURL } from '@/uni_modules/unix-openim-sdk'

const picture = {
  uuid: createBusinessUUID(),
  type: 'image/jpeg',
  size: 120000,
  width: 1280,
  height: 720,
  url: uploaded.url,
}

const message = await createImageMessageByURL({
  sourcePicture: picture,
  bigPicture: picture,
  snapshotPicture: picture,
  sourcePath: '',
})

URL 必须能被消息参与者访问,尺寸、大小和类型要与真实资源一致,不要把本地路径填入 URL 字段。Promise 成功只创建待发送对象;资源已经上传时使用 sendMessageNotOss()