【问题标题】:React-native-fast-images can't useReact-native-fast-images 无法使用
【发布时间】:2021-01-21 00:36:21
【问题描述】:

我正在尝试安装https://github.com/DylanVann/react-native-fast-image

我做了什么npm install --save react-native-fast-image-expo 因为我正在使用博览会

之后,我使用npm link react-native-fast-image-expo链接它

当我构建项目时,我得到 Unable to resolve "react-native-fast-image" from "App.js"

我尝试链接构​​建但没有任何效果

有必要吗?如果我从 API 获取图像,使用它?

【问题讨论】:

  • Expo 不允许链接本机模块,除非您已退出应用程序。如果你已经退出了,那么你应该使用react-native-fast-image,因为react-native-fast-image-expo 是一个一年多没有更新的旧分支,看起来它实际上并不支持未退出的 expo 应用程序。如果你还没有弹出你的应用,那么你就不能在你的项目中使用这个依赖。
  • 什么是弹射?以及它的作用对不起我是新人
  • 你应该通读这个问题的答案stackoverflow.com/questions/39170622/… 你应该阅读 react-native 和 expo 的文档,以便你熟悉每个可以做什么和不能做什么。
  • 弹出运行expo eject
  • 谢谢 世博会中的快速图像有什么替代品吗?在性能方面?

标签: reactjs react-native


【解决方案1】:

它并不完美,但您可以使用react-native-expo-image-cache 作为替代。

Example.js

import {Image} from "react-native-expo-image-cache";

// preview can be a local image or a data uri
const preview = { uri: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" };
const uri = "https://firebasestorage.googleapis.com/v0/b/react-native-e.appspot.com/o/b47b03a1e22e3f1fd884b5252de1e64a06a14126.png?alt=media&token=d636c423-3d94-440f-90c1-57c4de921641";
<Image style={{ height: 100, width: 100 }} {...{preview, uri}} />

从远程 URI 获取本地图像

import {CacheManager} from "react-native-expo-image-cache";

const {uri} = this.props;
const path = await CacheManager.get(uri).getPath();
// if path is undefined, the image download has failed 

【讨论】:

    【解决方案2】:

    我尝试了所有可能的解决方案,最终实现了自己的解决方案——非常快速和简单。在这里找到完整的解决方案https://dev.to/dmitryame/implementing-fast-image-for-react-native-expo-apps-1dn3

    这里的代码应该是不言自明的:

    import React, { useEffect, useState, useRef } from 'react'
    
    import { Image } from 'react-native'
    
    import * as FileSystem from 'expo-file-system'
    
    import PropTypes from 'prop-types'
    
    const CachedImage = props => {
      const { source: { uri }, cacheKey } = props
      const filesystemURI = `${FileSystem.cacheDirectory}${cacheKey}`
    
      const [imgURI, setImgURI] = useState(filesystemURI)
    
      const componentIsMounted = useRef(true)
    
      useEffect(() => {
        const loadImage = async ({ fileURI }) => {
          try {
            // Use the cached image if it exists
            const metadata = await FileSystem.getInfoAsync(fileURI)
            if (!metadata.exists) {
              // download to cache
              if (componentIsMounted.current) {
                setImgURI(null)
                await FileSystem.downloadAsync(
                  uri,
                  fileURI
                )
              }
              if (componentIsMounted.current) {
                setImgURI(fileURI)
              }
            }
          } catch (err) {
            console.log() // eslint-disable-line no-console
            setImgURI(uri)
          }
        }
    
        loadImage({ fileURI: filesystemURI })
    
        return () => {
          componentIsMounted.current = false
        }
      }, [])// eslint-disable-line react-hooks/exhaustive-deps
    
      return (
        <Image
        // eslint-disable-next-line react/jsx-props-no-spreading
          {...props}
          source={{
            uri: imgURI,
          }}
        />
      )
    }
    
    CachedImage.propTypes = {
      source: PropTypes.object.isRequired,
      cacheKey: PropTypes.string.isRequired,
    }
    
    export default CachedImage
    

    然后你像这样调用组件:

    <CachedImage
              source={{ uri: `${item.getThumbUrl}` }}
              cacheKey={`${item.id}t`}
              style={styles.thumbnail}
            />
    

    【讨论】:

    • 这只是一个链接的答案。请编辑您的答案并包含任何相关代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 2020-11-01
    • 2017-10-12
    • 2022-12-19
    相关资源
    最近更新 更多