欢迎光临,了解微信小程序开发,就上易用通!

微信小程序实现选择图片九宫格带预览

发布:2017-12-15 09:00浏览: 来源:网络 作者:tianshu

gallery.gif数据:依赖接口wx.upload、chooseImage与preview数据请求通过LeanCloud完成图片选择:https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html#wxchooseimageobject前端处理:1.保存images数 ...

 
 
 

 

微信小程序实现选择图片九宫格带预览(图1)

gallery.gif 
数据: 
依赖接口wx.upload、chooseImage与preview 
数据请求通过LeanCloud完成

图片选择:

https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html#wxchooseimageobject

前端处理:

1.保存images数组为已选择图片 
2.选择了更多图片后concat数组 
3.预览图集 
4.leancloud上传多图,目测顺序一致

js代码

 

		
  1. const AV = require('../../../utils/av-weapp.js')var that;
  2. Page({ data: { images: [], uploadedImages: [], imageWidth: getApp().screenWidth / 4 - 10
  3. }, onLoad: function (options) {
  4. that = this; var objectId = options.objectId; console.log(objectId);
  5. }, chooseImage: function () { // 选择图片
  6. wx.chooseImage({ sizeType: ['compressed'],
  7. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  8. success: function (res) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  9. var tempFilePaths = res.tempFilePaths; console.log(tempFilePaths);
  10. that.setData({ images: that.data.images.concat(tempFilePaths)
  11. });
  12. }
  13. })
  14. }, previewImage: function () { // 预览图集
  15. wx.previewImage({ urls: that.data.images
  16. });
  17. }, submit: function () { // 提交图片,事先遍历图集数组
  18. that.data.images.forEach(function (tempFilePath) { new AV.File('file-name', { blob: { uri: tempFilePath,
  19. },
  20. }).save().then( // file => console.log(file.url())
  21. function(file) { // 先读取
  22. var uploadedImages = that.data.uploadedImages;
  23. uploadedImages.push(file.url()); // 再写入
  24. that.setData({ uploadedImages: uploadedImages
  25. }); console.log(uploadedImages);
  26. }
  27. ).catch(console.error);
  28. });
  29. wx.showToast({ title: '评价成功', success: function () {
  30. wx.navigateBack();
  31. }
  32. });
  33. }, delete: function (e) { var index = e.currentTarget.dataset.index; var images = that.data.images;
  34. images.splice(index, 1);
  35. that.setData({ images: images
  36. });
  37. }
  38. })

wxml代码

 

		
  1. <view class="gallery">
  2. <view class="item" wx:for="{{images}}" wx:key="">
  3. <image style="width: {{imageWidth}}px; height: {{imageWidth}}px" src=" {{item}}" bindtap="previewImage" mode="aspectFill" />
  4. <!-- 删除按钮 -->
  5. <view class="delete" bindtap="delete" data-index="{{index}}"><image style="left: {{imageWidth / 2 - 10}}px;" src="/images/icon_delete.png" /></view>
  6. </view>
  7. <view class="item">
  8. <image style="width: {{imageWidth}}px; height: {{imageWidth}}px" src="/images/icon_add.png" class="button-upload" bindtap="chooseImage" />
  9. </view></view><button type="primary" bindtap="submit">提交</button>

wxss代码

 

		
  1. /*画廊*/.gallery { /*margin-bottom: 100rpx;*/
  2. display: flex; justify-content: flex-start; flex-wrap: wrap;
  3. }/*每张图片所占容器*/.item { position: relative; margin: 5px;
  4. }/*删除按钮*/.delete { position: absolute; height: 20px; bottom: 0; width: 100%; background: #ccc; opacity: .8;
  5. }.delete image { position: absolute; width: 20px; height: 20px;
  6. }

源码下载:https://git.oschina.net/dotton/lendoo-wx,本文涉及代码存于/pages/member/evalute文件夹中。






免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。