【问题标题】:How to upload images faster on iOS, when using takePictureAsync?使用 takePictureAsync 时,如何在 iOS 上更快地上传图像?
【发布时间】:2019-06-12 05:44:03
【问题描述】:

我们有一个使用 React Native 创建的应用程序,用户可以在其中拍照并将其保存到他的帐户中。所以我们将照片发送到我们的服务器。问题是,这在 iOS 上需要很长时间(大约 20 到 30 秒)。使用 Android-Build 会更快(大约 2 秒)。

我们曾尝试降低图片质量,但效果也不大。

takePicture = async function(camera) {

  const options = {
    quality: 0.5,
    fixOrientation: true,
    forceUpOrientation: true
  };

  const data = await camera.takePictureAsync(options);

  this.props.onCapture(data);

};

我们希望实现与 Android 上相同的上传时间。有人可以帮忙吗?

【问题讨论】:

    标签: ios react-native


    【解决方案1】:

    我编写了以下函数。拍摄图像后,它会返回原始和调整大小的图像,在 iOS 上大约需要 500KB。 它使用ImagePicker 包。

    const pickImage = async (index) => {
    const { status: cameraPerm } = await Permissions.askAsync(Permissions.CAMERA);
    const { status: cameraRollPerm } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
    
    import * as ImagePicker from 'expo-image-picker'
    if (cameraPerm === "granted" && cameraRollPerm === "granted") {
            let pickerResult;
            if (index == 0 || index == undefined) {
                pickerResult = await ImagePicker.launchCameraAsync({ allowsEditing: false, aspect: [4, 3], quality: 1 });
            }
            else if (index == 1) {
                pickerResult = await ImagePicker.launchImageLibraryAsync({ allowsEditing: false, aspect: [4, 3], quality: 1 });
            }
            if (!pickerResult.cancelled) {
                let resizedImage = await ImageManipulator.manipulateAsync(
                    pickerResult.uri, [{ resize: { width: 1200 } }],
                    { compress: 1, format: "jpg", base64: false });
                return [resizedImage.uri, pickerResult.uri];
            } else {
                return
            }
        } else {
            alert(Messages.userManagement.cameraPermissions);
            return
        }
    

    然后你可以像这样调用上面的方法。

    let [resizedImage, originalImage] = await pickImage();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-25
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多