【问题标题】:How to use Flickr api to make Image Gallery using React native如何使用 Flickr api 使用 React native 制作图片库
【发布时间】:2021-06-17 12:50:21
【问题描述】:

我已经用虚拟数据制作了一个画廊应用程序,现在我想使用 flickr 图像数据 但不知道如何使用这个 flickr api 并使用它获取数据。

我想从 flickr 网站的探索选项卡中获取 flickr “最近的照片” 在我的画廊中使用

这是我目前用于虚拟数据的数据文件的导入语句

let data = require('../Data.json')

这是通过 FlatList 实现的

  <FlatList
        horizontal={false}
        numColumns={3}
        data={data}
        renderItem={({ item }) => (
           
        <TouchableOpacity
          
            onPress={() => navigation.navigate("GalleryPhoto",item)}
          >
                 <View style={{flex:1}}>
        
          <Image
                source={{ uri: item.photo }}
                style={{ width: 140, height: 140 }}
          />
    
            </View>
          
          
          </TouchableOpacity>

这是代码的 github 存储库 https://github.com/adiruv/GalleryAppReactNative

【问题讨论】:

    标签: react-native api flickr


    【解决方案1】:

    1。注册闪烁。

    2。然后申请API key。

    3。使用包含您的 API 密钥的参数创建请求 URL。

    • 创建 URL 以搜索图像。

    const yourApiKey = 'your api comes here';
    
    const data = {
      method: 'flickr.photos.search',
      api_key: yourApiKey,
      text: 'cat', // Search Text
      sort: 'interestingness-desc',
      per_page: 12,
      license: '4',
      extras: 'owner_name,license',
      format: 'json',
      nojsoncallback: 1,
    };
    
    const parameters = new URLSearchParams(data);
    
    const url = `https://api.flickr.com/services/rest/?${parameters}`;
    console.log(url);

    4。使用该 URL 发出请求。

    const url = `https://api.flickr.com/services/rest/?${parameters}`;
    
    axios.get(url)
      .then(response => response.json())
      .then(data => {
        // We will impliment something later...
      });

    5。从 API 响应创建 ImageURL。

    const getFlickrImageURL = (photo, size) => {
      let url = `https://farm${photo.farm}.staticflickr.com/${photo.server}/${photo.id}_${
        photo.secret
      }`;
      if (size) {
        // Configure image size
        url += `_${size}`;
      }
      url += '.jpg';
      return url;
    };
    
    const url = `https://api.flickr.com/services/rest/?${parameters}`;
    
    axios.get(url)
      .then(response => response.json())
      .then(data => (
        // get an array of image-url
        data.photos.photo.map((photo) => {
          return getFlickrImageURL(photo, 'q');
        })
      ));

    6。显示图片

    • 现在您已经获得了来自 flicker API 的 imageURL 数组。
    • 您可以循环遍历数组并根据需要显示它。

    【讨论】:

    • 这里的数组名称是什么以及它被导出以供使用的确切位置...我正在使用 FlatList 来显示数组名称为“数据”的数据...抱歉我有点新为此,还要添加相关代码的github链接
    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2019-07-18
    相关资源
    最近更新 更多