我们有一个使用 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/