single_img.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { rpxToPx } from "@/utils/tools";
  2. import { View, Image } from "@tarojs/components";
  3. import Taro from "@tarojs/taro";
  4. import { useEffect, useState } from "react";
  5. export default function SingleImage(props: { url: string }) {
  6. const [loaded, setLoaded] = useState(false)
  7. const [width, setWidth] = useState(346)
  8. const [height, setHeight] = useState(346)
  9. const scale = '?x-oss-process=image/format,jpg'
  10. const kMax = 346
  11. // const
  12. //346
  13. useEffect(() => {
  14. Taro.downloadFile({
  15. url: props.url,
  16. success: (res) => {
  17. Taro.getImageInfo({
  18. src: res.tempFilePath,
  19. success: function (res) {
  20. console.log(res.width)
  21. console.log(res.height)
  22. if (res.width >= res.height) {
  23. setWidth(kMax)
  24. setHeight(kMax * res.height / res.width)
  25. }
  26. else {
  27. setHeight(kMax)
  28. setWidth(kMax * res.width / res.height)
  29. }
  30. setLoaded(true)
  31. },
  32. fail(res) {
  33. setLoaded(true)
  34. },
  35. })
  36. }
  37. })
  38. }, [])
  39. if (!loaded) return <View />
  40. return <Image src={props.url+scale}
  41. onClick={(e) => {
  42. if (process.env.TARO_ENV == 'weapp') {
  43. e.stopPropagation()
  44. }
  45. Taro.previewImage({
  46. current: props.url,
  47. urls: [props.url]
  48. })
  49. }}
  50. className="gray_bg"
  51. style={{
  52. width: rpxToPx(width),
  53. height: rpxToPx(height),
  54. marginBottom:rpxToPx(7)
  55. }} />
  56. }