Playing with WeChat Mini Programs (32) Image API

write picture description here

When selecting a picture, you can set whether the picture is the original picture and the source of the picture. This is also quite common, such as setting an avatar in the personal center, which can be used with wx.upLoadFile() API

Main method:

wx.chooseImage(object)

write picture description here

wxml

<!--监听按钮-->
<button type="primary" bindtap="listenerButtonChooseImage">点击我选择相册</button>
<!--通过数据绑定的方式动态获取js数据-->
<image src="{{source}}" mode="aspecFill" style="width: 640rpx; height: 640rpx"/>

js

Page({
  data:{
    // text:"这是一个页面"
    source: ''
  },
  /**
   * 选择相册或者相机 配合上传图片接口用
   */
  listenerButtonChooseImage: function() {
      var that = this;
      wx.chooseImage({
          count: 1,
          //original原图,compressed压缩图
          sizeType: ['original'],
          //album来源相册 camera相机 
          sourceType: ['album', 'camera'],
          //成功时会回调
          success: function(res) {
              //重绘视图
              that.setData({
                  source: res.tempFilePaths
              })
          }
      })
  },

wx.previewImage(object)

write picture description here

This is another weird API. I really don't know how to use this. First imitate the official use, but it has no effect. I understand how to supplement my own use.

wxml

<!--图片预览-->
<button type="primary" bindtap="listenerButtonPreviewImage">展示图片</button>

js

Page({
  data:{
    // text:"这是一个页面"
    source: ''
  },

  /**
   * 预览图片 又一个奇葩接口
   */
  listenerButtonPreviewImage: function() {
      wx.previewImage({
          current: 'http://img.souutu.com/2016/0511/20160511055648316.jpg',
          urls: [
              'http://img.souutu.com/2016/0511/20160511055648316.jpg',
               'http://img.souutu.com/2016/0511/20160511055650751.jpg',
               'http://img.souutu.com/2016/0511/20160511054928658.jpg'
               ],
               //这根本就不走
               success: function(res) {
                   console.log(res);
               },
               //也根本不走
               fail: function() {
                   console.log('fail')
               }
      })
  }


})

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325504109&siteId=291194637