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

使用 URL 创建图片消息

根据已经上传的图片资源信息创建待发送消息。

复制

createImageMessageByURL() 适用于图片已经由业务上传服务取得远端 URL 的场景。原图、大图和缩略图可以使用不同资源;只有三者确实相同时才复用同一份信息。

参数说明

参数类型是否必填说明
sourcePathstring原始本地路径;只有远端资源时传空字符串。
sourcePictureOpenIMPicture原图信息。
bigPictureOpenIMPicture大图信息。
snapshotPictureOpenIMPicture缩略图信息。

每个图片对象应填写实际资源信息:

字段类型说明
uuidstring资源唯一标识,优先使用上传结果返回的值。
typestring图片 MIME 类型。
sizenumber文件大小,单位为字节。
widthnumber图片宽度,单位为像素。
heightnumber图片高度,单位为像素。
urlstring已上传图片的可访问地址。
import { createImageMessageByURL } from '@/uni_modules/unix-openim-sdk'

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

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

资源 URL、类型、大小和尺寸必须来自实际上传结果。创建成功后使用发送已上传的媒体消息,避免再次进入 SDK 上传流程。