Taro.chooseImage(option)
Selects an image from the local album or takes a photo with the camera.
Type
(option: Option) => Promise<SuccessCallbackResult>
Parameters
Option
| Property | Type | Required | Description |
|---|---|---|---|
| count | number | No | The maximum number of images allowed |
| sizeType | ("original" | "compressed")[] | No | The size of the select image |
| sourceType | ("album" | "camera" | "user" | "environment")[] | No | The source of the image |
| complete | (res: any) => void | No | The callback function used when the API call completed (always executed whether the call succeeds or fails) |
| fail | (res: any) => void | No | The callback function for a failed API call |
| success | (res: Result) => void | No | The callback function for a successful API call |
sizeType
Valid values of object.sizeType
| Property | Description |
|---|---|
| original | Original image |
| compressed | compressed |
sourceType
Valid values of object.sourceType
| Property | Description |
|---|---|
| album | Selects an image from the album |
| camera | Takes a photo with the camera |
| user | Using the front camera (Only H5) |
| environment | Using the rear camera (Only H5) |
SuccessCallbackResult
| Property | Type | Description |
|---|---|---|
| tempFilePaths | string[] | The list of local temporary file paths to images |
| tempFiles | ImageFile[] | The local temporary file list for images |
| errMsg | string | Call result |
ImageFile
List of local temporary files for images
| Property | Type | Required | Description |
|---|---|---|---|
| path | string | Yes | The path to the local temporary file |
| size | number | Yes | The size of a local temporary file, in bytes |
| type | string | No | The MIME type of the file. (Only H5) |
| originalFileObj | File | No | The original browser File object. (Only H5) |
API Support
| API | WeChat Mini-Program | H5 | React Native |
|---|---|---|---|
| ImageFile.type | ✔️ | ||
| ImageFile.originalFileObj | ✔️ |
Sample Code
Taro.chooseImage({
count: 1, // The default value is 9
sizeType: ['original', 'compressed'], // You can specify whether the image is original or compressed, both are available by default.
sourceType: ['album', 'camera'], // You can specify whether the source is an album or a camera, both are available by default.
success: function (res) {
// tempFilePath can be used as the src property of the img tag to display images.
var tempFilePaths = res.tempFilePaths
}
})
API Support
| API | WeChat Mini-Program | Baidu Smart-Program | Alipay Mini-Program | H5 | React Native |
|---|---|---|---|---|---|
| Taro.chooseImage | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |