|
作者:何东_hd,来自原文地址 一:相册选择,拍照小程序中获取图片可通过两种方式得到,第一种是直接打开微信内部自己的样式,第一格就是相机拍照,后面是图片,第二种是弹框提示用户是要拍照还是从相册选择,下面一一来看。 选择相册要用到wx.chooseImage(OBJECT)函数,具体参数如下:
直接来看打开相机相册的代码: Page({ data: { tempFilePaths: '' }, onLoad: function () { }, chooseimage: function () { var that = this; wx.chooseImage({ count: 1, // 默认9 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 that.setData({ tempFilePaths: res.tempFilePaths }) } }) },})
方法一效果图如下:
个人认为第二种用户体验要好一点,效果如下:
点击获取弹框提示,代码如下: Page({ data: { tempFilePaths: '' }, onLoad: function () { }, chooseimage: function () { var that = this; wx.showActionSheet({ itemList: ['从相册中选择', '拍照'], itemColor: "#CED63A", success: function (res) { if (!res.cancel) { if (res.tapIndex == 0) { that.chooseWxImage('album') } else if (res.tapIndex == 1) { that.chooseWxImage('camera') } } } }) }, chooseWxImage: function (type) { var that = this; wx.chooseImage({ sizeType: ['original', 'compressed'], sourceType: [type], success: function (res) { console.log(res); that.setData({ tempFilePaths: res.tempFilePaths[0], }) } }) }})
文件的临时路径,在小程序本次启动期间可以正常使用,如需持久保存,需在主动调用 wx.saveFile,在小程序下次启动时才能访问得到。 布局文件: <button style="margin:30rpx;" bindtap="chooseimage">获取图片</button><image src="{{tempFilePaths }}" catchTap="chooseImageTap" mode="aspectFit" style="width: 100%; height: 450rpx" />
官方文档: 二:toast等弹框提示微信小程序中toast消息提示框只有两种显示的效果,就是成功和加载,使用wx.showToast(OBJECT)。 看下有关参数说明:
代码很简单: wx.showToast({ title: '成功', icon: 'succes', duration: 1000, mask:true })
mask属性好像并没有起作用。有一点值得注意的是提示的延迟时间是有限制的,最大10000毫秒。 还有一个函数是wx.hideToast(),这个是隐藏toast,主要用于显示加载提示的时候用到,如: wx.showToast({ title: '加载中', icon: 'loading', duration: 10000})setTimeout(function(){ wx.hideToast()},2000)
本来加载时间是10000毫秒的,然后2000毫秒的时候就隐藏了,这个具体情况而定了哈。 第二个弹窗是模态弹窗:wx.showModal(OBJECT) 参数如下:
这个跟我们Android里面的Dialog相似,效果如下:
代码如下: wx.showModal({ title: '提示', content: '模态弹窗', success: function (res) { if (res.confirm) { console.log('用户点击确定') }else{ console.log('用户点击取消') } } })
最后一个是操作菜单:wx.showActionSheet(OBJECT) 这个函数我们在上一篇博文用过,这里说一下也无妨。先看一下参数介绍:
success有一个返回参数:
这里直接贴官方实例了: wx.showActionSheet({ itemList: ['A', 'B', 'C'], success: function(res) { console.log(res.tapIndex) }, fail: function(res) { console.log(res.errMsg) }})
效果图:
这里有个bug,弹出showActionSheet之后,点击取消或者阴影处,会执行完fail之后,继续执行success函数,所以如果在这里要做跳转或者什么操作,还是自己定义比较好。 就是这么简单,赶紧动起来试试吧。 |