微信小程序开发-图片(媒体)

相关官方文档链接:媒体-图片

对于选择图片,别的不说,先来一波代码再分析。

wxml:

<button bindtap='chooseImg' type='primary'>选择图片</button>
  <view class="imgView" style='width:100px;height:100px;margin:10px auto;border:1px solid #000;'>
  <image src='{{imgSrc}}' style='width:100%;height:100%;'></image>
</view>

js:

chooseImg: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function (res) {
        console.log(res);
        var tempFilePaths = res.tempFilePaths;
        var tempFiles = res.tempFiles;
        console.log(tempFilePaths);
        console.log(tempFiles);
        that.setData({
          imgSrc: tempFilePaths
        })
      },
    })
  }

上传图片成功。


预览图片:

wxml:

<view>预览图片</view>
<view class="content">  
    <view wx:for="{{imglist}}" wx:for-item="image" class="previewimg">  
        <image src="{{image}}" data-src="image" bindtap="previewImage"></image>  
    </view>  
</view>

js:

data:{
    imglist: [
      'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=621999311,1727379372&fm=27&gp=0.jpg',   
      'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=499261909,3433823994&fm=27&gp=0.jpg',  
      'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2822716709,4140849071&fm=27&gp=0.jpg'   
    ]
  }
获取图片信息:

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

保存图片到系统相册:

save:function(){
    var that =this;
    wx.saveImageToPhotosAlbum({
      filePath: that.data.imgSrc,
      success(res) {
        console.log("保存到系统成功")
      }
    })
  }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值