ios - 使用 takePictureAsync 时如何在 iOS 上更快地上传图像?

标签 ios react-native

我们有一个使用 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 上大约需要 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();

关于ios - 使用 takePictureAsync 时如何在 iOS 上更快地上传图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56555345/

相关文章:

ios - 是否允许隐藏 iAds,即使它们可用?

ios - onFocusChange 在 SwiftUI tvOS 中未触发

ios - AVMetadataItem 的编码

react-native - 启动应用程序时出现世博会错误。 "ApiV2Error: Account not found. "

react-native - 我想生成我的 react 应用程序的 apk,但我不想安装 android studio

javascript - React Native 最小/准系统设置

ios - Objective-C 如果这个 UIScrollView 是这个大小

javascript - 单击标记时尝试导航到另一个屏幕

react-native - React Native 版本不匹配错误 - Expo

iphone - 为什么尝试用drawRect绘制彩色圆圈时颜色是透明的?