开始

定义数据
image.png

  1. userpic:''

image.png

image.png

  1. userpic:'../../static/demo/userpic/11.jpg'

https://uniapp.dcloud.net.cn/api/media/image.html#chooseimage
image.png

image.png

  1. // 修改头像
  2. changeimg(){
  3. uni.chooseImage({
  4. count: 1, //默认9
  5. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  6. // sourceType: ['album'], //从相册选择
  7. success: function (res) {
  8. console.log(JSON.stringify(res));
  9. this.userpic=res.tempFilePaths;
  10. }
  11. });;
  12. }

image.png

  1. <view class="u-f-ac" @tap="changeimg">

image.png

image.png

image.png
选中头像被拉伸
image.png

image.png

  1. <image :src="userpic" mode="aspectFill" lazy-load="true"></image>

image.png

本节代码

html5端用base64的方法去展示图片的形式
https://www.yuque.com/meirenchangduancuiyanlou/pawgo4/gztqko

引入了一个js库。
image.png

image.png

  1. <template>
  2. <view class="body">
  3. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  4. <view>头像</view>
  5. <view class="u-f-ac" @tap="changeimg">
  6. <image :src="userpic" mode="aspectFill" lazy-load="true"></image>
  7. <view class="icon iconfont icon-bianji1"></view>
  8. </view>
  9. </view>
  10. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  11. <view>昵称</view>
  12. <view class="u-f-ac">
  13. <!-- <view>昵称</view> -->
  14. <input type="text" v-model="username" />
  15. <view class="icon iconfont icon-bianji1"></view>
  16. </view>
  17. </view>
  18. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  19. <view>性别</view>
  20. <view class="u-f-ac" @tap="changeOne('sex')">
  21. <view>{{sex}}</view>
  22. <view class="icon iconfont icon-bianji1"></view>
  23. </view>
  24. </view>
  25. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  26. <view>生日</view>
  27. <view class="u-f-ac">
  28. <view>1987-02-07</view>
  29. <view class="icon iconfont icon-bianji1"></view>
  30. </view>
  31. </view>
  32. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  33. <view>情感</view>
  34. <view class="u-f-ac" @tap="changeOne('qg')">
  35. <view>{{qg}}</view>
  36. <view class="icon iconfont icon-bianji1"></view>
  37. </view>
  38. </view>
  39. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  40. <view>职业</view>
  41. <view class="u-f-ac" @tap="changeOne('job')">
  42. <view>{{job}}</view>
  43. <view class="icon iconfont icon-bianji1"></view>
  44. </view>
  45. </view>
  46. <view class="user-set-userinfo-list u-f-ac u-f-jsb">
  47. <view>家乡</view>
  48. <view class="u-f-ac">
  49. <view>中国广州</view>
  50. <view class="icon iconfont icon-bianji1"></view>
  51. </view>
  52. </view>
  53. <button class="user-set-btn" :class="{'user-set-btn-disable':disabled}" :loading="loading" @tap="submit"
  54. type="primary" :disabled="disabled">完成</button>
  55. </view>
  56. </template>
  57. <script>
  58. import {
  59. pathToBase64
  60. } from '../../js_sdk/mmmm-image-tools/index.js'
  61. let sex=['不限','男','女'];
  62. let qg=['秘密','未婚','已婚'];
  63. let job=['秘密','IT','老师'];
  64. export default {
  65. data() {
  66. return {
  67. userpic: '../../static/demo/userpic/11.jpg',
  68. username:'哈哈哈',
  69. sex:"不限",
  70. qg:"未婚",
  71. job:"IT"
  72. }
  73. },
  74. computed:{
  75. avatorSrc(){
  76. return this.userpic.replace(/[\r\n]/g, '');
  77. }
  78. },
  79. methods: {
  80. changeOne(val){
  81. let arr=[];
  82. switch(val){
  83. case 'sex':
  84. arr=sex;
  85. break;
  86. case 'qg':
  87. arr=qg;
  88. break;
  89. case 'job':
  90. arr=job;
  91. break;
  92. }
  93. uni.showActionSheet({
  94. itemList:arr,
  95. success: res => {
  96. switch(val){
  97. case 'sex':
  98. this.sex=arr[res.tapIndex];
  99. break;
  100. case 'qg':
  101. this.qg=arr[res.tapIndex];
  102. break;
  103. case 'job':
  104. this.job=arr[res.tapIndex];
  105. break;
  106. }
  107. console.log(res);
  108. }
  109. })
  110. },
  111. // 修改头像
  112. changeimg() {
  113. uni.chooseImage({
  114. count: 1, //默认9
  115. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  116. // sourceType: ['album'], //从相册选择
  117. success: res => {
  118. console.log(JSON.stringify(res));
  119. // this.userpic=res.tempFilePaths;
  120. //解决跨越问题,让通过微信上传的图片你能够展示在浏览器前端
  121. uni.getImageInfo({
  122. src: res.tempFilePaths[0],
  123. success: (path) => {
  124. pathToBase64(path.path).then(base64 => {
  125. // console.log(base64); // 这就是转为base64格式的图片
  126. console.log('base64赋值');
  127. this.userpic=base64;
  128. // console.log(this.userpic);
  129. })
  130. .catch(error => {
  131. console.error(error)
  132. })
  133. }
  134. });
  135. }
  136. });;
  137. },
  138. submit(){
  139. }
  140. }
  141. }
  142. </script>
  143. <style>
  144. @import url('@/common/form.css');
  145. .user-set-userinfo-list {
  146. padding: 20upx;
  147. border-bottom: 1upx solid #F4F4F4;
  148. }
  149. /* 左侧的文字 */
  150. .user-set-userinfo-list>view:first-child {
  151. font-size: 32upx;
  152. font-weight: bold;
  153. color: #9B9B9B;
  154. }
  155. /* 右侧头像 */
  156. .user-set-userinfo-list>view:last-child>image {
  157. width: 80upx;
  158. height: 80upx;
  159. border-radius: 100%;
  160. }
  161. /* 右侧文本框 */
  162. .user-set-userinfo-list>view:last-child>input{
  163. text-align: right;
  164. }
  165. /* 最右侧的修改的铅笔图标 */
  166. .user-set-userinfo-list>view:last-child>view:last-of-type {
  167. margin-left: 20upx;
  168. color: #9B9B9B;
  169. }
  170. </style>

结束