
微信小程序 拍照和相机选择详解
前言:
小程序中获取图片可通过两种方式得到,第一种是直接打开微信内部自己的样式,第一格就是相机拍照,后面是图片,第二种是d框提示用户是要拍照还是从相册选择,下面一一来看。
选择相册要用到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 }) } }) }, })
方法一效果图如下:
个人认为第二种用户体验要好一点,效果如下:
点击获取d框提示,代码如下:
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" />
微信小程序调用ps命令步骤如下:1、需要在PS这个软件中,需要制作好要插入微信小程序开发者工具的图片。
2、可以选择WEB方式进行导出。
3、直接导出PNG能够被微信小程序开发者工具所兼容。
4、导入云存储,然后就能够在微信小程序开发者工具中引用了。
1、在微信开发者工具中,打开app.json文件,在pages数组中增加show.wxml页面相关文件的代码,以加粗显示,代码如下:
{
"pages":[
"pages/index/index",
"pages/show/show",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#ccc",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle":"black"
}
}
2、在index.wxml文件中,在类为usermotto的view组件中添加绑定属性catchtap='enterShow',以加粗显示,代码如下:
<!--index.wxml-->
<view class="container">
<view class="userinfo">
<button wx:if="{{!hasUserInfo &&canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo">获取头像昵称 </button>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
<view class="usermotto" catchtap='enterShow'>
<text class="user-motto">{{motto}}</text>
</view>
</view>
3、在index.js文件中,将data中motto的值改为“点击进入”。编写实现跳转的自定义函数enterShow,加粗显示,代码如下:
//index.js
//获取应用实例
const app = getApp()
Page({
data: {
motto: '点击进入',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
//事件处理函数
enterShow:function(){
wx.navigateTo({
url: '../show/show',
})
4、 在show.wxml中,输入跳转后页面显示的信息,代码如下:
<view>
<text>这是跳转后的页面</text>
</view>
5、然后在index.xwml中点击测试就可以了。
说明:在上面的页面跳转自定义函数enterShow中,也可以使用wx.redirectTo实现跳转。两者的区别:redirectTo将关闭当前页面,跳转到指定页面,页面左上角没有返回的箭头按钮;而navigateTo将保留页面,跳转到指定页面,页面左上角有返回的箭头按钮。
扩展资料其实在小程序后台很早就有个wx.openUrl的函数,普通开发者没有调用权限,这次微信给自家的小程序开放权限,旨在测试这一功能可能的风险。因为这一功能如果全部开放,将会给小程序用户带来很大的安全隐患。居心不良的开发者可能会将用户引流至一些不安全页面。
小程序的审核难度也会变得很大。因为微信除了审核小程序本身的页面跳转和内容,还需要审核外链的链接,并且还不一定能够审核清楚。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)