从相机卷[ios]反应本机获取照片名称

时间:2016-05-17 18:42:44

标签: ios react-native

我正在使用本机进行ios开发 我很新的回应原生和我很难理解如何从照片库获取图像文件名。 我正在尝试使用cameraroll

2 个答案:

答案 0 :(得分:2)

返回的对象采用以下形状:

node{
    "type":
    "group_name": 
    "image": {
                "uri": 
                "height": 
                "width": 
                "isStored": 
              },
              .....

没有名称属性,您可以在RCTCameraRollManager.m中添加名称属性,如下面的代码;

NSString *name = [result defaultRepresentation].filename;
 @"node": @{
              @"type": [result valueForProperty:ALAssetPropertyType],
              @"group_name": [group valueForProperty:ALAssetsGroupPropertyName],
              @"image": @{
                @"uri": uri,
                @"name" : name,
...

答案 1 :(得分:0)

filename在v0.57中可用,但是它们仅不会更改JS端的PropTypes。但它是在v0.58中添加的。

添加到V0.57:

node_module/react-native/libraries/CameraRoll/CameraRoll.js中覆盖以下代码。您可以复制和粘贴相同的代码,也可以仅添加filename个部分。


type GetPhotosReturn = Promise<{
  edges: Array<{
    node: {
      type: string,
      group_name: string,
      image: {
        uri: string,
        height: number,
        width: number,
        isStored?: boolean,
        filename: string,
        playableDuration: number,
      },
      timestamp: number,
      location?: {
        latitude?: number,
        longitude?: number,
        altitude?: number,
        heading?: number,
        speed?: number,
      },
    },
  }>,
  page_info: {
    has_next_page: boolean,
    start_cursor?: string,
    end_cursor?: string,
  },
}>;

/**
 * Shape of the return value of the `getPhotos` function.
 */
const getPhotosReturnChecker = createStrictShapeTypeChecker({
  edges: PropTypes.arrayOf(
    /* $FlowFixMe(>=0.66.0 site=react_native_fb) This comment suppresses an
     * error found when Flow v0.66 was deployed. To see the error delete this
     * comment and run Flow. */
    createStrictShapeTypeChecker({
      node: createStrictShapeTypeChecker({
        type: PropTypes.string.isRequired,
        group_name: PropTypes.string.isRequired,
        image: createStrictShapeTypeChecker({
          uri: PropTypes.string.isRequired,
          height: PropTypes.number.isRequired,
          width: PropTypes.number.isRequired,
          filename: PropTypes.string
          isStored: PropTypes.bool,
          playableDuration: PropTypes.number.isRequired,
        }).isRequired,
        timestamp: PropTypes.number.isRequired,
        location: createStrictShapeTypeChecker({
          latitude: PropTypes.number,
          longitude: PropTypes.number,
          altitude: PropTypes.number,
          heading: PropTypes.number,
          speed: PropTypes.number,
        }),
      }).isRequired,
    }),
  ).isRequired,
  page_info: createStrictShapeTypeChecker({
    has_next_page: PropTypes.bool.isRequired,
    start_cursor: PropTypes.string,
    end_cursor: PropTypes.string,
  }).isRequired,
});

然后在node_modules/@types/react-native/index.d.ts中覆盖以下代码:

export interface GetPhotosReturnType {
    edges: {
        node: {
            type: string;
            group_name: string;
            image: {
                uri: string;
                height: number;
                width: number;
                filename: string,
                isStored?: boolean;
            };
            timestamp: number;
            location: {
                latitude: number;
                longitude: number;
                altitude: number;
                heading: number;
                speed: number;
            };
        };
    }[];

    page_info: {
        has_next_page: boolean;
        start_cursor?: string;
        end_cursor?: string;
    };
}