14、微信小程序——上传、预览、长按删除图片

1、上传多图

多图上传的关键在于——渲染

Page({
    data:{
        images:[]
    },
    upload(){
        wx.chooseImage({
          count:9,   //获取图片数量
          sizeType: ['original','compressed'], //图片尺寸:原图、压缩图
          sourceType: ['camera','album'], //图片来源:相册、相机
          success: (res) => {
              let tempFilePath=res.tempFilePaths
              this.setData({
                  images:tempFilePath
              })
          }
        })
    }
})

多图上传的关键在于——渲染

<button bindtap="upload">点击上传</button>
<view class="X-box">
    <view wx:for="{{images}}" class="box" wx:key="index">
        <image src="{{item}}" mode="" />
    </view>
</view>
/* pages/upload/index.wxss */
.X-box {
    width: 100vw;
    .box {
        float: left;
        width: 33%;
        display: flex;
        flex-direction: row;
        justify-content: space-around;

        image {
            width: 90px;
            height: 90px;
            // margin-bottom: 10px;
            margin-top: 10px;
        }
    }
}

2、预览图片(wx.previewImage)

 重点:

        1、给 image 设置一个 自定义属性 index

        2、保存该方法的 this 指向

        3、重点利用 data 中的存放图片的集合数组 

<view class="X-box">
    <view wx:for="{{images}}" class="box" wx:key="index">
        <image src="{{item}}" mode=""  bindtap="look" data-index="{{index}}"/>
    </view>
</view>
 look(e){
        console.log(e);
        let index=e.target.dataset.index   //通过 index 属性拿到当前被选中的图片
        let that=this  //保存 this 指向
        wx.previewImage({  
          current:that.data.images[index],  //获取所要预览图片的链接 
          urls:that.data.images  //上传的全部图片
        })
    },

 

 

3、长按删除图片

 重点:

    1、保存 this 指向

    2、拿到data 数据中的 存放图片集合的数组

    3、声明变量 index 用来标志被选中的图片

    4、删除提示 wx.showModal 

 del(e){
        var that=this
        var images=that.data.images
        var index=e.currentTarget.dataset.index
        wx.showModal({
            title:'提示',
            content:'确定要删除此图片吗?',
            success:(res)=>{
                if(res.confirm){
                    images.splice(index,1)
                }else if(res.calcel){
                    return false
                }
                that.setData({
                    images
                })
            }
        })
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值