【问题标题】:Expo - ImagePicker aspect on iOS not working. Square onlyExpo - iOS 上的 ImagePicker 方面不起作用。仅正方形
【发布时间】:2018-05-08 19:12:38
【问题描述】:

imagePicker 组件适用于 iOS 和 Android。

纵横比在 android 上完美运行,但在 iOS 上不适用。我正在使用 create-react-native-app,我不想弹出并使用另一个包。

我需要创建三个裁剪选项 - 横向、方形和纵向。现在我只能得到方形图像。

从相机拍照时,我得到宽度和高度的正方形尺寸,但从图库中选择时得到原始图像尺寸。

我正在使用一段示例代码。让我知道可以做些什么来解决这个问题,或者我可以用另一个包来实现这个而不弹出。非常感谢。

import React, { Component } from 'react';
import { Button, Text, ScrollView, StyleSheet } from 'react-native';
import { ImagePicker, Permissions, Constants } from 'expo';

export default class App extends Component {
state = {
result: null,
};

askPermissionsAsync = async () => {
    await Permissions.askAsync(Permissions.CAMERA);
    await Permissions.askAsync(Permissions.CAMERA_ROLL);
    // you would probably do something to verify that permissions
    // are actually granted, but I'm skipping that for brevity
};

useLibraryHandler = async () => {
    await this.askPermissionsAsync();
    let result = await ImagePicker.launchImageLibraryAsync({
        allowsEditing: true,
        aspect: [12, 3],
        base64: false,
    });
    console.log(result.width, result.height, "result");
    this.setState({ result });
};

useCameraHandler = async () => {
    await this.askPermissionsAsync();
    let result = await ImagePicker.launchCameraAsync({
        allowsEditing: true,
        aspect: [12, 3],
        base64: false,
    });
    console.log(result.width, result.height, "result");
    this.setState({ result });
};

render() {
    return (
        <ScrollView style={{flex: 1}} contentContainerStyle={styles.container}>
            <Button title="launchCameraAsync" onPress={this.useCameraHandler} />
            <Button
                title="launchImageLibraryAsync"
                onPress={this.useLibraryHandler}
            />
            <Text style={styles.paragraph}>
                {JSON.stringify(this.state.result)}
            </Text>
        </ScrollView>
    );
}
}

【问题讨论】:

    标签: react-native expo


    【解决方案1】:

    IOS

    中返回的只有 方形图像

    如展会中提到的docs

    aspect (array) -- 一个包含两个条目的数组[x, y],指定允许用户编辑图像时要保持的纵横比(通过传递allowsEditing: true)。

    这仅适用于 Android,因为在 iOS 裁剪矩形 始终是正方形

    因此,您需要使用react-native-image-resizer 或任何其他替代包手动调整图像大小。

    【讨论】:

    • 我一遍又一遍地阅读这个。我不敢相信我错过了这个。真对不起!您能否向我解释一下裁剪器窗口是否可以使用此软件包固定为宽屏格式?还是只是使用方形裁剪窗口并在裁剪后调整输出图像的大小?如果可能的话,你能否向我解释一下如何做到这一点?谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-07-11
    • 2022-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 2012-12-17
    • 1970-01-01
    • 2015-12-23
    相关资源
    最近更新 更多