• [转]微信小程序开发之从相册获取图片 使用相机拍照 本地图片上传


    本文转自:http://blog.csdn.net/qq_31383345/article/details/53014610

    今天遇到微信小程序的用户头像设置功能,做笔记.

    先上gif:

    再上代码:

    小demo,代码很简单.

    1.index.wxml

    [html] view plain copy
     
    1. <!--index.wxml-->  
    2. <button style="margin:30rpx;" bindtap="chooseimage">获取图片</button>  
    3. <image src="{{tempFilePaths }}" mode="aspecFill" style=" 100%; height: 450rpx"/>  

    2.index.js

    [javascript] view plain copy
     
    1. //index.js  
    2. //获取应用实例  
    3. var app = getApp()  
    4. Page({  
    5.   data: {  
    6.     tempFilePaths: ''  
    7.   },  
    8.   onLoad: function () {  
    9.   },  
    10.   chooseimage: function () {  
    11.     var _this = this;  
    12.     wx.chooseImage({  
    13.       count: 1, // 默认9  
    14.       sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有  
    15.       sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有  
    16.       success: function (res) {  
    17.         // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片  
    18.         _this.setData({  
    19.           tempFilePaths:res.tempFilePaths  
    20.         })  
    21.       }  
    22.     })  
    23.   }  
    24. })  

    API 说明:


    这里说说sourcetype.默认是从相册获取和使用相机拍照,跟微信现在选择图片的界面一样,第一格是拍照,后面的是相册照片.

    这里注意:返回的是图片在本地的路径.如果需要将图片上传到服务器,需要用到另一个API.

    示例代码:

    [javascript] view plain copy
     
    1. wx.chooseImage({  
    2.   success: function(res) {  
    3.     var tempFilePaths = res.tempFilePaths  
    4.     wx.uploadFile({  
    5.       url: 'http://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址  
    6.       filePath: tempFilePaths[0],  
    7.       name: 'file',  
    8.       formData:{  
    9.         'user': 'test'  
    10.       },  
    11.       success: function(res){  
    12.         var data = res.data  
    13.         //do something  
    14.       }  
    15.     })  
    16.   }  
    17. })  

    我的博客:http://blog.csdn.net/qq_31383345

  • 相关阅读:
    [JSOI2010]解题报告+2010~2011小结
    有用的东西(emacs配置和bzoj数据下载网址)
    [JSOI2011]解题报告
    [JSOI2010]旅行题解
    [BOI2007]Mokia题解
    分块总结
    统计数字
    爬不出去的水井
    采药
    沙漠储油点
  • 原文地址:https://www.cnblogs.com/freeliver54/p/7002390.html
Copyright © 2020-2023  润新知