当前位置:   article > 正文

鸿蒙开发之图片选择器_鸿蒙开发js上传图片

鸿蒙开发js上传图片

一、使用

系统的图片选择器真的非常友好,这个绝对要赞一下。

  1. pickPhotos() {
  2. //初始化一个photopicker
  3. let photoPicker = new picker.PhotoViewPicker()
  4. //maxSelectNumber最多选择多少张(默认值为50,最大值为500
  5. //MIMEType 选择文件的类型
  6. photoPicker.select({maxSelectNumber:9,MIMEType:picker.PhotoViewMIMETypes.IMAGE_TYPE}).then(val => {
  7. //通过val.photoUris就可以拿到图片的Uri数组了
  8. //val.photoUris
  9. }).catch(err => {
  10. Prompt.showToast({message:'获取图片失败'})
  11. })
  12. }

二、实现图片删除

长按图片,给图片添加一个抖动的动画。这里借助了z轴旋转动画实现。

  1. import picker from '@ohos.file.picker'
  2. import Prompt from '@system.prompt'
  3. /*
  4. * 开发中动画实现上遇到一个问题不能停止动画的问题
  5. * 一开始想的是直接操作旋转的角度和z轴的值,但是发现有些gridItem不生效还会动
  6. * 群友提供了一个思路:
  7. * 在动画finish回调中不断的重置旋转的角度和z轴的值达到开启关闭动画
  8. * */
  9. @Entry
  10. @Component
  11. struct OfficialPhotoPickPage2 {
  12. @State imageUris: string[] = ["https://tse1-mm.cn.bing.net/th/id/OIP-C.LWCXuZysYTsReGHGVjKBvAAAAA?w=211&h=211&c=7&r=0&o=5&dpr=1.9&pid=1.7"
  13. ,"https://tse3-mm.cn.bing.net/th/id/OIP-C.mmUrUJJ8RtIXNpFFU1XqsgAAAA?w=211&h=211&c=7&r=0&o=5&dpr=1.9&pid=1.7"
  14. ,"https://c-ssl.dtstatic.com/uploads/blog/202307/03/9WSX9PPas8AzV3V.thumb.400_0.jpeg"
  15. ,'https://tse2-mm.cn.bing.net/th/id/OIP-C.V7iv7AMzhwagJ3oyhiHvnAAAAA?w=210&h=210&c=7&r=0&o=5&dpr=1.9&pid=1.7']
  16. stroller: Scroller = new Scroller()
  17. //监听该属性来动态改变grid的高度
  18. @State gridHeight: number = 200
  19. //监听操作按钮显示与否设置动画属性
  20. @State showDelBtn: boolean = false
  21. //angle不断改变值来达到动画的停止与启用
  22. @State angle: number = 0
  23. //设置动画z轴值
  24. @State imageRotate: number = 0
  25. //抖动动画
  26. startAnim(){
  27. animateTo({
  28. duration:100,
  29. curve:Curve.EaseInOut,
  30. iterations:1,
  31. playMode:PlayMode.Alternate,
  32. onFinish:()=>{//在结束的时候通过按钮显示与否改变动画属性
  33. if( this.showDelBtn ){
  34. this.angle = this.angle === 0 ? 5 : 0
  35. this.imageRotate = this.imageRotate === 0 ? 1 : 0;
  36. this.startAnim();
  37. }
  38. }
  39. },()=>{
  40. })
  41. }
  42. build() {
  43. Column() {
  44. Grid() {
  45. ForEach(this.imageUris,(item,index) => {
  46. GridItem() {
  47. Stack() {
  48. Image(item)
  49. .borderWidth(3)
  50. .borderRadius(20)
  51. .width(100)
  52. .height(100)
  53. .margin({top:20})
  54. .rotate({
  55. z:this.imageRotate,
  56. angle:this.angle
  57. })
  58. Button(){
  59. //warning '这里记得替换成关闭图片'
  60. Image($rawfile('guanbi.png'))
  61. .width(20)
  62. .height(20)
  63. }
  64. .fontColor(Color.White)
  65. .fontSize(15)
  66. .width(this.showDelBtn ? 20 : 0)
  67. .height(this.showDelBtn ? 20 : 0)
  68. .margin({top:15,right:0,left:80})
  69. .onClick(() => {
  70. //删除某个图片,并关闭动画
  71. this.showDelBtn = false
  72. this.imageUris.splice(index,1)
  73. this.gridHeight = ((this.imageUris.length/3)+(this.imageUris.length%3>0?1:0))*130
  74. this.angle = 0
  75. this.imageRotate = 0
  76. })
  77. }
  78. .alignContent(Alignment.TopEnd)
  79. }
  80. .gesture(LongPressGesture({repeat:true}).onAction(() => {
  81. //长按显示按钮,并开始抖动动画
  82. this.showDelBtn = true
  83. this.angle = 5
  84. this.imageRotate = 1
  85. this.startAnim();
  86. }))
  87. })
  88. }
  89. .columnsGap(10)
  90. .rowsGap(10)
  91. .width('90%')
  92. .backgroundColor(Color.Gray)
  93. .margin({top:20})
  94. Button('请选择图片')
  95. .type(ButtonType.Capsule)
  96. .width(150)
  97. .height(40)
  98. .margin({top:30})
  99. .onClick(() => {
  100. this.pickPhotos()
  101. })
  102. }
  103. .width('100%')
  104. .height('100%')
  105. }
  106. //图片选择
  107. pickPhotos() {
  108. let photoPicker = new picker.PhotoViewPicker()
  109. photoPicker.select({maxSelectNumber:9,MIMEType:picker.PhotoViewMIMETypes.IMAGE_TYPE}).then(val => {
  110. //现有数据与选择的数据拼接
  111. this.imageUris = [...this.imageUris,...val.photoUris]
  112. //更新grid的高度
  113. this.gridHeight = ((this.imageUris.length/3)+(this.imageUris.length%3>0?1:0))*100
  114. }).catch(err => {
  115. Prompt.showToast({message:'获取图片失败'})
  116. })
  117. }
  118. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/329779
推荐阅读
相关标签
  

闽ICP备14008679号