RN在android/ios手机剪切图片的操作

之前写过一个React Native调用摄像头画面及拍照和保存图片到相册全流程但是这个仅限于调用摄像头拍照并保存图片,今天再写一个版本的操作,这个博客目前实现的有三点操作:

  1. 调用摄像头拍照
  2. 对照片进行剪切
  3. 从相册选取图片

功能上面来说有两点:

  1. 点击按钮可以对摄像头进行拍照,拍完照会自动跳转到编辑页面,编辑完后图片会显示到页面上面
  2. 相册选取图片,选择完了自动跳转到图片编辑页面,编辑完了会显示到页面上

这个兼容android和ios

下面就是实现步骤

安装下面的依赖包

js 复制代码
yarn add react-native-image-crop-picker

在RN项目下android/app/src/main/AndroidManifest.xml文件内加入以下权限申请(调用摄像头权限)

js 复制代码
...
<uses-permission android:name="android.permission.CAMERA"/>
...

在RN项目下的ios/项目名/Info.plist文件内新增以下权限申请(调用相册和摄像头)

js 复制代码
  ...
  <key>NSPhotoLibraryUsageDescription</key>
  <string>App需要您的同意,才能访问相册</string>
  <key>NSCameraUsageDescription</key>
  <string>App需要您的同意,才能访问相机</string>
  ...

由于下载了新依赖包,需要在ios文件夹打开终端执行下pod install安装ios相关依赖

在RN项目新加个页面,放入以下代码

js 复制代码
import React, { useState } from 'react';
import { View, Button, Image } from 'react-native';
import ImagePicker from 'react-native-image-crop-picker';

const MyComponent = () => {
  const [image, setImage] = useState(null);

  const handleCameraPress = async () => {
    try {
      const pickedImage = await ImagePicker.openCamera({
        cropping: true,
        cropperCircleOverlay: false,
        width: 300,
        height: 300,
        cropperToolbarTitle: '',
        cropperToolbarColor: 'black',
        cropperActiveWidgetColor: '#ffffff',
        cropperStatusBarColor: 'black',
        cropperToolbarWidgetColor: '#ffffff',
        cropperToolbarVisible: false,
        hideBottomControls: true,
        freeStyleCropEnabled: false
      });
      setImage(pickedImage.path);
    } catch (error) {
      console.log('Error:', error);
    }
  };

  const handleGalleryPress = async () => {
    try {
      const pickedImage = await ImagePicker.openPicker({
        cropping: true,
        cropperCircleOverlay: false,
        width: 300,
        height: 300,
        cropperToolbarTitle: '',
        cropperToolbarColor: 'black',
        cropperActiveWidgetColor: '#ffffff',
        cropperStatusBarColor: 'black',
        cropperToolbarWidgetColor: '#ffffff',
        cropperToolbarVisible: false,
        hideBottomControls: true,
        freeStyleCropEnabled: false
      });
      setImage(pickedImage.path);
    } catch (error) {
      console.log('Error:', error);
    }
  };

  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      {image && <Image source={{ uri: image }} style={{ width: 200, height: 200 }} />}
      <Button title="拍照并裁剪" onPress={handleCameraPress} />
      
      <Button title="从相册选择并裁剪" onPress={handleGalleryPress} />
    </View>
  );
};

export default MyComponent;


PS:如果这里有爆红先不用管,因为path是ImagePicker返回回来的对象里面的属性,编辑器不知道,默认就是未识别的属性,反正代码能跑通

然后直接看页面运行就好,下面贴几个效果图

页面样式

截图页面样式

选取相册图片

相关推荐
wanhengidc几秒前
云手机可以息屏挂手游吗?
运维·网络·安全·游戏·智能手机
Y4090011 小时前
数据库基础知识——聚合函数、分组查询
android·数据库
没有了遇见5 小时前
Android 原生定位(替代高德 / 百度等三方定位)<终极版本>
android
2501_916008896 小时前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_915921436 小时前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
CYRUS_STUDIO7 小时前
LLVM 全面解析:NDK 为什么离不开它?如何亲手编译调试 clang
android·编译器·llvm
CYRUS_STUDIO7 小时前
静态分析神器 + 动态调试利器:IDA Pro × Frida 混合调试实战
android·逆向
wanhengidc7 小时前
云手机的空间会占用本地内存吗
科技·游戏·智能手机
wanhengidc9 小时前
网页版的云手机都有哪些优势?
运维·网络·安全·游戏·智能手机
g_i_a_o_giao9 小时前
Android8 binder源码学习分析笔记(一)
android·java·笔记·学习·binder·安卓源码分析