【问题标题】:React Native - How to maintain image ratio with width = "100%"?React Native - 如何保持宽度=“100%”的图像比例?
【发布时间】:2021-03-16 02:52:29
【问题描述】:

我的数据库中有图片 uri,我正在使用 width: "100%"

渲染它们

由于图像可以是 1080x1080、750x1080 和 1920x1080,我需要找到一种方法来渲染它们保持纵横比o...我的意思是,如果我有设备宽度为600px,图片宽度为600px(“100%”),高度自动调整,以免丢失比例。

我试过这个CSS

  image: {
    width: "100%",
    aspectRatio: 1,
  },

但仅适用于方形图像 (1080x1080)。 我该怎么做一个height: "auto" in react native,以便其他图像的大小调整为正确的方面?

重要提示:我不知道图片的尺寸,我只知道图片的宽度必须等于设备宽度。

【问题讨论】:

    标签: javascript css reactjs react-native


    【解决方案1】:

    您必须采取一些步骤。

    首先,您需要知道图像的确切大小以及使用Image.getSizeImage.getSizeWithHeaders 所做的事情

    让我在下面举一个例子。

    export interface ImageDetli {
      width: number;
      height: number;
      url: string;
    }
    
    const ImageRenderer = ({
      url
    }: {
      url: string
    }) => {
      const [windowWidth, setWindowWidth] = useState(Dimensions.get("window").width);
      const [windowHeight, setWindowHeight] = useState(Dimensions.get("window").height);
      const [loading, setLoading] = useState(true); // untill we load the image fully
      const [image, setImage] = useState(undefined as ImageDetli | undefined)
      const getSize = async(url: string) => {
    
        return new Promise((resolve, reject) => {
          // this is very imported so you can keep an eye on the size of the image
          Image.getSize(url, (width, height) => {
            resolve({
              url: url,
              width: width > windowWidth ? windowWidth : width,
              height: height > windowHeight ? windowHeight : height
            });
          }, async(error) => {
            console.log("image not loaded: " + url);
            console.log(error)
            resolve(undefined);
    
          })
        }) as Promise <ImageDetli | undefined>
      }
    
      useEffect(() => {
        (async() => {
          var imageDetali = await getSize(url);
          if (!imageDetali){
            // load an empty Image or somthing
          }else {
            setImage(imageDetali);
          }
          setLoading(false)
        })();
      }, [])
    
    
      return (
      <>
      {loading?(<ActivityIndicator size="large"color="#0000ff"/>)
      :
      (
      <View style={{ width: "100%", height: "100%", alignItems: "center", 
      justifyContent: "center" }}>
      <Image source={{uri: image.url}}
      style={{ width:image.width, height image.height, resizeMode: "contain" }} />
     </View>
      )}
      </>
      )
    }

    我希望你明白这一点,你可能会问你是否需要了解更多。

    【讨论】:

    • 我已经对此进行了测试,并且工作正常,谢谢。另外,我实现了另一种直接使用 CSS 工作的方式 { width: "100%", height: "100%", resizeMode: "contain", } 因为我不需要知道图像的大小
    • 如果你用 css 来做,它在设备上不会很好用。我已经测试过了:)
    • 你的意思是在生产中吗?或者它会在某些设备中失败?另外,看看这个零食snack.expo.io/@victoriomolina/b361af 它似乎在两个平台上都可以使用。但是......我认为这可能不适用于旧设备或类似的东西
    • 不,如果您使用 css 进行操作,那么您可以尝试使用比窗口尺寸小得多的图像,您会发现图像看起来不太好。图像 raito 将不再正确。
    猜你喜欢
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2023-03-21
    • 2018-06-21
    • 2017-12-24
    • 2018-02-25
    • 1970-01-01
    相关资源
    最近更新 更多