WeChat Open Platform

Mini Programs Documentation

Images

wx.chooseImage(OBJECT)

Select an image from a local album or use camera to take a photo.

OBJECT parameter descriptions:

Parameter Type Required Description
count Number No Maximum number of images that can be selected, default is 9
sizeType StringArray No original (original image), compressed (compressed image), both have defaults
sourceType StringArray No album (image selected from album), camera (camera used), both have defaults
success Function Yes Local image file path list tempFilePaths returned if successful
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Note: Temporary file paths can be used as normal during this Mini Program startup. wx.saveFile needs to be called actively if files need to be saved permanently. They can only be accessed the next time the Mini Program is launched.

success return parameter descriptions:

Parameter Description
tempFilePaths Local image file path list

Sample code:

wx.chooseImage({
  count: 1, // Default 9
  sizeType: ['original', 'compressed'], // Can specify whether it is the original or compressed image, both have defaults
  sourceType: ['album', 'camera'], // Can specify whether the source is an album or camera, both have defaults
  success: function (res) {
    // Returns the local file path list for the selected photo, tempFilePath can be used as the img tag's src attribute to display the image
    var tempFilePaths = res.tempFilePaths
  }
})

wx.previewImage(OBJECT)

Preview image.

OBJECT parameter descriptions:

Parameter Type Required Description
current String No The links for the currently displayed images. If these are not filled in, the default is the first image in the urls
urls StringArray Yes List of links for images that need to be previewed
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code:

wx.previewImage({
  current: '', // http link for currently displayed image
  urls: [] // List of http links for images that need to be previewed
})

wx.getImageInfo(OBJECT)

Gets image information

OBJECT parameter descriptions:

Parameter Type Required Description
src String Yes Image path, can be a relative path, temporary file path, storage file path, or network image path
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

success return parameter descriptions:

Parameter Type Description
width Number Image width, unit px
height Number Image height, unit px
path String Local path for returned image

Sample code:

wx.getImageInfo({
  src: 'images/a.jpg',
  success: function (res) {
    console.log(res.width)
    console.log(res.height)
  }
})

wx.chooseImage({
  success: function (res) {
    wx.getImageInfo({
      src: res.tempFilePaths[0],
      success: function (res) {
        console.log(res.width)
        console.log(res.height)
      }
    })
  }
})