当前位置:   article > 正文

#uni-app# u-avatar-cropper实现选择图片裁剪,设置用户头像uview (附源码)

u-avatar

思路:

第一步:首先需要在项目里安装uView框架

》》》点击跳转uview安装教程https://blog.csdn.net/ZHENGCHUNJUN/article/details/119913012?spm=1001.2014.3001.5501icon-default.png?t=L9C2https://blog.csdn.net/ZHENGCHUNJUN/article/details/119913012?spm=1001.2014.3001.5501

第二步:在page.josn里面,引入u-avatar-cropper的路径

  1. {
  2. "path": "uview-ui/components/u-avatar-cropper/u-avatar-cropper",
  3. "style": {
  4. "navigationBarTitleText": "头像裁剪",
  5. "navigationBarBackgroundColor": "#000000"
  6. }
  7. }

第三步:排版

里面会用到唤起菜单的插件u-action-sheet (注意要放在版块外面,以免排版错乱)

》》》点击跳转到actionSheet参数

  1. <template>
  2. <view>
  3. <view class="personal">
  4. <view class="title">头像</view>
  5. <view class="name">
  6. <view class="item">
  7. <image :src="imgsrc"></image>
  8. </view>
  9. <u-icon name="arrow-right" color="#666666" class="right-icon"></u-icon>
  10. </view>
  11. </view>
  12. <u-action-sheet :list="headlist" v-model="show"></u-action-sheet>
  13. </view>
  14. </template>

第四步:设置点击事件,唤起操作菜单

  1. <template>
  2. <view>
  3. <view class="personal" @click="show = true"> //触发点击事件以后,操作菜单为激活状态
  4. <view class="title">头像</view>
  5. <view class="name">
  6. <view class="item">
  7. <image :src="imgsrc"></image>
  8. </view>
  9. <u-icon name="arrow-right" color="#666666" class="right-icon"></u-icon>
  10. </view>
  11. </view>
  12. <u-action-sheet :list="headlist" v-model="show" @click="toCut()"></u-action-sheet>
  13. </view>
  14. </template>
  15. export default {
  16. data() {
  17. return {
  18. imgsrc: '/static/1.jpg',
  19. show: false, //操作菜单为隐藏状态
  20. headlist: [{ //操作菜单的参数设置
  21. text: '选择图片',
  22. color: '#666666',
  23. fontSize: 35
  24. }, ],
  25. }
  26. },
  27. }
  28. }
  29. </script>

第五步:在操作菜单这个组件里,设置点击事件,进行裁剪

  1. <template>
  2. <view>
  3. <u-action-sheet :list="headlist" v-model="show" @click="toCut()"></u-action-sheet>
  4. </view>
  5. </template>
  6. methods: {
  7. toCut() { //触发点击事件以后,跳转到裁剪头像的页面
  8. this.$u.route({
  9. url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
  10. // 内部已设置以下默认参数值,可不传这些参数
  11. params: {
  12. // 输出图片宽度,高等于宽,单位px
  13. destWidth: 300,
  14. // 裁剪框宽度,高等于宽,单位px
  15. rectWidth: 300,
  16. // 输出的图片类型,如果'png'类型发现裁剪的图片太大,改成"jpg"即可
  17. fileType: 'jpg',
  18. }
  19. })
  20. }
  21. }

第六步:进行监听图片裁剪结果,以及传递到服务器

  1. created() {
  2. // 监听从裁剪页发布的事件,获得裁剪结果
  3. uni.$on('uAvatarCropper', path => {
  4. this.imgsrc = path;
  5. // 可以在此上传到服务端
  6. uni.uploadFile({
  7. url: '', //后期自己接后端接口
  8. filePath: path,
  9. name: 'file',
  10. complete: (res) => {
  11. console.log(res);
  12. }
  13. });
  14. })
  15. },

源码展示:

  1. <template>
  2. <view>
  3. <view class="personal" @click="show = true">
  4. <view class="title">头像</view>
  5. <view class="name">
  6. <view class="item">
  7. <image :src="imgsrc"></image>
  8. </view>
  9. <u-icon name="arrow-right" color="#666666" class="right-icon"></u-icon>
  10. </view>
  11. </view>
  12. <u-action-sheet :list="headlist" v-model="show" @click="toCut()"></u-action-sheet>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. imgsrc: '/static/1.jpg',
  20. show: false,
  21. headlist: [{
  22. text: '选择图片',
  23. color: '#666666',
  24. fontSize: 35
  25. }, ],
  26. }
  27. },
  28. created() {
  29. // 监听从裁剪页发布的事件,获得裁剪结果
  30. uni.$on('uAvatarCropper', path => {
  31. this.imgsrc = path;
  32. // 可以在此上传到服务端
  33. uni.uploadFile({
  34. url: '',
  35. filePath: path,
  36. name: 'file',
  37. complete: (res) => {
  38. console.log(res);
  39. }
  40. });
  41. })
  42. },
  43. methods: {
  44. toCut() {
  45. this.$u.route({
  46. url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
  47. // 内部已设置以下默认参数值,可不传这些参数
  48. params: {
  49. // 输出图片宽度,高等于宽,单位px
  50. destWidth: 300,
  51. // 裁剪框宽度,高等于宽,单位px
  52. rectWidth: 300,
  53. // 输出的图片类型,如果'png'类型发现裁剪的图片太大,改成"jpg"即可
  54. fileType: 'jpg',
  55. }
  56. })
  57. }
  58. }
  59. }
  60. </script>
  61. <style lang="scss">
  62. .personal {
  63. display: flex;
  64. align-items: center;
  65. justify-content: space-between;
  66. width: 90%;
  67. margin: 50upx auto 35upx;
  68. .title {
  69. font-size: 30upx;
  70. color: #333333;
  71. }
  72. .name {
  73. display: flex;
  74. align-items: center;
  75. .item {
  76. height: 52upx;
  77. font-size: 22upx;
  78. color: #666666;
  79. display: flex;
  80. align-items: center;
  81. image {
  82. width: 70upx;
  83. height: 70upx;
  84. border-radius: 50%;
  85. }
  86. }
  87. .right-icon {
  88. padding-left: 15upx;
  89. }
  90. }
  91. }
  92. </style>

效果展示:

 

 

 

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号