You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS端@react-native-community/cameraroll调用getPhotos、getAlbums无返回图片问题

解决方案
    1. 配置iOS端相册访问权限
      打开iOS项目的info.plist文件,添加以下权限配置:
    <!-- 相册读取权限描述,会展示在权限申请弹窗中 -->
    <key>NSPhotoLibraryUsageDescription</key>
    <string>需要访问您的相册以选取照片</string>
    <!-- 若需要新增/修改相册内容才需要添加以下配置 -->
    <key>NSPhotoLibraryAddUsageDescription</key>
    <string>需要保存照片到您的相册</string>
    
    1. 补全接口必填参数
      iOS端调用getPhotos时不允许传空配置对象,必须明确指定first(单次拉取的资源数量)和assetType(资源类型)参数,且必须先申请相册权限,确认授权通过后再调用查询接口,示例代码:
    import { PermissionsIOS, Platform } from 'react-native';
    import CameraRoll from "@react-native-community/cameraroll";
    
    // 权限申请逻辑
    const requestIOSPhotoPermission = async () => {
      if (Platform.OS !== 'ios') return true;
      const status = await PermissionsIOS.request('photo');
      return status === 'granted';
    }
    
    // 查询数据逻辑
    const fetchPhotoData = async () => {
      const hasPermission = await requestIOSPhotoPermission();
      if (!hasPermission) return;
      const photoRes = await CameraRoll.getPhotos({
        first: 100, // 单次最多拉取100张资源
        assetType: 'Photos', // 只查询图片,可选值:Photos/Videos/All
      });
      console.log('照片查询结果', photoRes);
    
      const albumRes = await CameraRoll.getAlbums({
        assetType: 'Photos'
      });
      console.log('相册查询结果', albumRes);
    }
    
    1. 处理有限访问权限场景
      iOS14及以上版本支持用户给App授予「仅允许访问指定照片」的有限权限,这种情况下如果用户没有选择任何照片,接口也会返回空列表。你可以在权限申请后判断权限状态,如果是limited状态,可以引导用户重新选择可访问的照片,或者申请全量相册访问权限。
    1. 确认库版本兼容性
      如果你使用的@react-native-community/cameraroll版本低于4.0.0,请升级到最新稳定版,旧版本对iOS14+的权限适配存在缺陷,会导致即使授权后也返回空数据。

内容的提问来源于stack exchange,提问作者Fullmetal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 06:15:02