【小程序】媒体API能力集成指南——图片API
ty.chooseImage
从本地相册选择图片或使用相机拍照, 可用 chooseMedia 替代此方法
权限:[scope.camera, scope.writePhotosAlbum] 关联 API:[chooseMedia]
需引入
BaseKit
,且在>=2.1.2
版本才可使用
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
count | number | 否 | 最多可以选择的图片张数 | |
sizeType | array | 否 | sizeType ['original', 'compressed'] | |
sourceType | array | 否 | 选择图片的来源 ['album', 'camera'] | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 |
object.success 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
tempFilePaths | string[] | 图片的本地临时文件路径列表 (本地路径) |
tempFiles | TempFileCB[] | 图片的本地临时文件列表 |
TempFileCB
结构属性 | 类型 | 说明 |
---|---|---|
path | string | 本地临时文件路径 (本地路径) |
size | number | 本地临时文件大小,单位 B |
object.fail 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
errorMsg | string | 插件错误信息 |
errorCode | string | 错误码 |
innerError | object | 插件外部依赖错误信息 {errorMsg: string, errorCode: string } |
函数定义示例
/*** 从本地相册选择图片或使用相机拍照, 可用chooseMedia替代此方法**权限:[scope.camera, scope.writePhotosAlbum]*关联API:[chooseMedia]*/
export function chooseImage(params?: {/** 最多可以选择的图片张数 */count?: number;/** sizeType ['original', 'compressed'] */sizeType?: string[];/** 选择图片的来源 ['album', 'camera'] */sourceType?: string[];complete?: () => void;success?: (params: {/** 图片的本地临时文件路径列表 (本地路径) */tempFilePaths: string[];/** sizeType ['original', 'compressed'] */tempFiles?: TempFileCB[];}) => void;fail?: (params: {errorMsg: string;errorCode: string | number;innerError: {errorCode: string | number;errorMsg: string;};}) => void;
}): void;
ty.chooseMedia
拍摄或从手机相册中选择图片或视频
权限:[scope.camera, scope.writePhotosAlbum]
需引入
BaseKit
,且在>=2.5.0
版本才可使用
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
count | number | 否 | 最多可以选择的文件数 | |
mediaType | string | 否 | 选择类型, 默认图片 'image' 只能拍摄图片或从相册选择图片 'video' 只能拍摄视频或从相册选择视频 | |
sourceType | array | 否 | 图片和视频选择的来源, 默认['album', 'camera'] 'album' 从相册选择 'camera' 使用相机拍摄 | |
maxDuration | number | 否 | 拍摄视频最长拍摄时间,单位秒。默认 10s 时间范围为 3s 至 60s 之间。不限制相册。 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 |
object.success 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
type | string | 文件类型 'image' 图片 'video' 视频 |
tempFiles | array | 本地临时文件列表 |
object.fail 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
errorMsg | string | 插件错误信息 |
errorCode | string | 错误码 |
innerError | object | 插件外部依赖错误信息 {errorMsg: string, errorCode: string } |
函数定义示例
/*** 拍摄或从手机相册中选择图片或视频**权限:[scope.camera, scope.writePhotosAlbum]*/
export function chooseMedia(params?: {/** 最多可以选择的文件数 */count?: number;/*** 选择类型, 默认图片* 'image' 只能拍摄图片或从相册选择图片* 'video' 只能拍摄视频或从相册选择视频*/mediaType?: string;/*** 图片和视频选择的来源, 默认['album', 'camera']* 'album' 从相册选择* 'camera' 使用相机拍摄*/sourceType?: string[];/*** 拍摄视频最长拍摄时间,单位秒。默认10s* 时间范围为 3s 至 60s 之间。不限制相册。*/maxDuration?: number;complete?: () => void;success?: (params: {/*** 文件类型* 'image' 图片* 'video' 视频*/type: string;/** 本地临时文件列表 */tempFiles?: TempMediaFileCB[];}) => void;fail?: (params: {errorMsg: string;errorCode: string | number;innerError: {errorCode: string | number;errorMsg: string;};}) => void;
}): void;
👉 立即开发。
ty.getImageInfo
获取图片信息
需引入
BaseKit
,且在>=2.4.3
版本才可使用
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
src | string | 是 | 图片的路径,支持网络路径、本地路径 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 |
object.success 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
width | number | 图片原始宽度,单位 px。不考虑旋转。 |
height | number | 图片原始高度,单位 px。不考虑旋转。 |
orientation | string | 拍照时设备方向 合法值 说明 up 默认方向(手机横持拍照),对应 Exif 中的 1。或无 orientation 信息。 up-mirrored 同 up,但镜像翻转,对应 Exif 中的 2 down 旋转 180 度,对应 Exif 中的 3 down-mirrored 同 down,但镜像翻转,对应 Exif 中的 4 left-mirrored 同 left,但镜像翻转,对应 Exif 中的 5 right 顺时针旋转 90 度,对应 Exif 中的 6 right-mirrored 同 right,但镜像翻转,对应 Exif 中的 7 left 逆时针旋转 90 度,对应 Exif 中的 8 |
type | string | 图片格式。 合法值 说明 unknown 未知格式 jpeg jpeg 压缩格式 png png 压缩格式 gif gif 压缩格式 tiff tiff 压缩格式 |
object.fail 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
errorMsg | string | 插件错误信息 |
errorCode | string | 错误码 |
innerError | object | 插件外部依赖错误信息 {errorMsg: string, errorCode: string } |
函数定义示例
/*** 获取图片信息*/
export function getImageInfo(params: {/** 图片的路径,支持网络路径、本地路径 */src: string;complete?: () => void;success?: (params: {/** 图片原始宽度,单位px。不考虑旋转。 */width: number;/** 图片原始高度,单位px。不考虑旋转。 */height: number;/*** 拍照时设备方向* 合法值 说明* up 默认方向(手机横持拍照),对应 Exif 中的 1。或无 orientation 信息。* up-mirrored 同 up,但镜像翻转,对应 Exif 中的 2* down 旋转180度,对应 Exif 中的 3* down-mirrored 同 down,但镜像翻转,对应 Exif 中的 4* left-mirrored 同 left,但镜像翻转,对应 Exif 中的 5* right 顺时针旋转90度,对应 Exif 中的 6* right-mirrored 同 right,但镜像翻转,对应 Exif 中的 7* left 逆时针旋转90度,对应 Exif 中的 8*/orientation: string;/*** 图片格式。* 合法值 说明* unknown 未知格式* jpeg jpeg压缩格式* png png压缩格式* gif gif压缩格式* tiff tiff压缩格式*/type: string;}) => void;fail?: (params: {errorMsg: string;errorCode: string | number;innerError: {errorCode: string | number;errorMsg: string;};}) => void;
}): void;
ty.chooseCropImage
从本地相册选择图片或使用相机拍照(可裁剪)
需引入
BaseKit
,且在>=2.2.4
版本才可使用
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
sourceType | array | 否 | 选择图片的来源 ['album', 'camera'],默认都支持 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 |
object.success 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
path | string | 文件路径 |
object.fail 回调参数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
errorMsg | string | 插件错误信息 |
errorCode | string | 错误码 |
innerError | object | 插件外部依赖错误信息 {errorMsg: string, errorCode: string } |
函数定义示例
/*** 从本地相册选择图片或使用相机拍照(可裁剪)*/
export function chooseCropImage(params?: {/** 选择图片的来源 ['album', 'camera'],默认都支持 */sourceType?: string[];complete?: () => void;success?: (params: {/** 文件路径 */path: string;}) => void;fail?: (params: {errorMsg: string;errorCode: string | number;innerError: {errorCode: string | number;errorMsg: string;};}) => void;
}): void;
ty.previewImage
在新页面中全屏预览图片。
需引入
BaseKit
,且在>=2.1.2
版本才可使用
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
urls | array | 是 | 需要预览的图片链接列表 | |
current | number | 是 | 当前显示图片的链接 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 |
函数定义示例
/*** 在新页面中全屏预览图片。*/
export function previewImage(params: {/** 需要预览的图片链接列表 */urls: string[];/** 当前显示图片的链接 */current: number;complete?: () => void;success?: (params: null) => void;fail?: (params: {errorMsg: string;errorCode: string | number;innerError: {errorCode: string | number;errorMsg: string;};}) => void;
}): void;
👉 立即开发。