如何在 React Native 中优化图像

新手上路,请多包涵

通过相机拍摄的照片太大,无法在 React Native 中高效上传和下载。

在 React Native 中是否有用于压缩 PNG 图像文件的 api 或库?

原文由 Learner 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答

https://github.com/bamlab/react-native-image-resizer 提供了一个 API 来调整本地图像的大小。

它允许您指定:

  • 最大尺寸(同时保留纵横比)和;
  • 输出质量(仅适用于 JPEG)

API

 import ImageResizer from 'react-native-image-resizer';

ImageResizer.createResizedImage(imageUri, newWidth, newHeight, compressFormat, quality).then((resizedImageUri) => {
  // resizeImageUri is the URI of the new image that can now be displayed, uploaded...
}).catch((err) => {
  // Oops, something went wrong. Check that the filename is correct and
  // inspect err to get more details.
});

原文由 Learner 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您使用 react-native-image-picker 上传图片,您可以设置 maxWidth、maxHeight 或图片质量来减小尺寸。

  const options = {
                    title: 'Select Picture',
                    storageOptions: {
                        skipBackup: true,
                        path: 'images',

                    },
                    maxWidth: 500,
                    maxHeight: 500,
                    quality: 0.5
                };
ImagePicker.showImagePicker(options, resolve, reject);

原文由 Shehzad Osama 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题