zoukankan      html  css  js  c++  java
  • 微信小程序保存图片功能实现

    小程序保存图片功能实现

    wxml:

    <view class="previewImage" style="display:{{previewImage}}" bindtap="closePrev">
        <image src="{{img.url}}" mode="aspectFit" style="100%;height:100%;" catchlongtap="saveImg"></image>
    </view>
    <action-sheet hidden="{{saveImage}}" bindchange="actionSheetChange">
        <block>
            <action-sheet-item class="item" bindtap="downloadFile" data-image-href="{{img.url}}">保存图片</action-sheet-item>
        </block>
        <action-sheet-cancel class="cancel">取消</action-sheet-cancel>
    </action-sheet>

    wxss:

    .previewImage{
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: #000;
    }

    js:

        previewImg:function(){
            this.setData({
                previewImage:"block"
            });
        },
        saveImg:function(){
            longTouch = true;
            this.setData({
                saveImage:false
            })
        },
        closePrev:function(){
            if(longTouch){
                longTouch = false;
            }else{
                this.setData({
                    previewImage:"none"
                });
            }
        },
        actionSheetChange:function(){
            this.setData({
                saveImage:!this.data.saveImage
            })
        },
        downloadFile:function(e){
            var imgUrl = e.currentTarget.dataset.imageHref;
            var that = this;
            wx.downloadFile({
                  url: imgUrl,
                  type: 'image',
                  success:function(res){
                      var tempFilePath = res.tempFilePath;
                    console.log(tempFilePath);
                    wx.saveFile({
                          tempFilePath:tempFilePath,
                          success:function(res){
                            var savedFilePath = res.savedFilePath;
                            console.log(savedFilePath);
                            that.setData({
                                saveImage:true
                            });
                          },
                          fail:function(res){
                              console.log(res);
                          }
                    });
                  },
                  fail:function(res){
                      console.log(res);
                  }
            });
        }
  • 相关阅读:
    数据库范式
    java String.split()用法
    1.4 IoC控制反转
    利用shrinkwrap锁定依赖版本
    清晨开启电脑自动拉取项目更新
    JS如何获取屏幕、浏览器及网页高度宽度?
    Navigator 对象,能够清楚地知道浏览器的相关信息
    iconfont 转换为图标字体
    VS code的搜索、替换与正则替换
    点九图制作方法
  • 原文地址:https://www.cnblogs.com/lovebing/p/9474583.html
Copyright © 2011-2022 走看看