single_img.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 kMax = 346
  10. // const
  11. //346
  12. useEffect(() => {
  13. Taro.downloadFile({
  14. url: props.url,
  15. success: (res) => {
  16. Taro.getImageInfo({
  17. src: res.tempFilePath,
  18. success: function (res) {
  19. console.log(res.width)
  20. console.log(res.height)
  21. if (res.width >= res.height) {
  22. setWidth(kMax)
  23. setHeight(kMax * res.height / res.width)
  24. }
  25. else {
  26. setHeight(kMax)
  27. setWidth(kMax * res.width / res.height)
  28. }
  29. setLoaded(true)
  30. }
  31. })
  32. }
  33. })
  34. }, [])
  35. if (!loaded) return <View />
  36. return <Image src={props.url}
  37. onClick={(e) => {
  38. if (process.env.TARO_ENV == 'weapp') {
  39. e.stopPropagation()
  40. }
  41. Taro.previewImage({
  42. current: props.url,
  43. urls: [props.url]
  44. })
  45. }}
  46. style={{
  47. width: rpxToPx(width),
  48. height: rpxToPx(height),
  49. marginBottom:rpxToPx(7)
  50. }} />
  51. }