Skip to content

Commit 4d12fac

Browse files
ChenlingasMxhy
authored andcommitted
fix(ImagePicker): 图片保存功能
1 parent 7ae3323 commit 4d12fac

File tree

10 files changed

+105
-18
lines changed

10 files changed

+105
-18
lines changed

example/base/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"react-native-gesture-handler": "2.8.0",
1919
"react-native-root-siblings": "4.1.1",
2020
"react-native-image-picker":"^5.3.1",
21-
"react-native-image-viewing":"~0.2.2"
21+
"react-native-image-viewing":"~0.2.2",
22+
"@react-native-camera-roll/camera-roll":"~5.3.1"
2223
},
2324
"devDependencies": {
2425
"@babel/core": "~7.20.7",

example/examples/android/app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
package="com.examples">
33

44
<uses-permission android:name="android.permission.INTERNET" />
5-
5+
<uses-permission android:name="android.permission.CAMERA" />
6+
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
7+
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
8+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
69
<application
710
android:name=".MainApplication"
811
android:label="@string/app_name"
912
android:icon="@mipmap/ic_launcher"
1013
android:roundIcon="@mipmap/ic_launcher_round"
1114
android:allowBackup="false"
12-
android:theme="@style/AppTheme">
15+
android:theme="@style/AppTheme"
16+
android:requestLegacyExternalStorage="true"
17+
>
1318
<activity
1419
android:name=".MainActivity"
1520
android:label="@string/app_name"

example/examples/ios/examples/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
</dict>
3838
<key>NSCameraUsageDescription</key>
3939
<string>This app requires access to the camera.</string>
40+
<key>NSPhotoLibraryAddUsageDescription</key>
41+
<string>This app requires access to the photo.</string>
4042
<key>NSLocationWhenInUseUsageDescription</key>
4143
<string></string>
4244
<key>UILaunchStoryboardName</key>

example/examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"react-native-image-picker":"^5.3.1",
2424
"react-native-image-viewing":"~0.2.2",
2525
"@uiw/react-native-image-picker": "3.2.3",
26-
"@react-native-community/cameraroll":"^4.1.2"
26+
"@react-native-camera-roll/camera-roll":"~5.3.1"
2727
},
2828
"devDependencies": {
2929
"@babel/core": "~7.20.7",

example/examples/tsconfig.json

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
/* Basic Options */
44
"target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
55
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
6-
"lib": [
7-
"es2017"
8-
] /* Specify library files to be included in the compilation. */,
6+
"lib": ["es2017"] /* Specify library files to be included in the compilation. */,
97
"allowJs": true /* Allow javascript files to be compiled. */,
108
// "checkJs": true, /* Report errors in .js files. */
119
"jsx": "react-native" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,
@@ -41,7 +39,8 @@
4139
"baseUrl": "./" /* Base directory to resolve non-absolute module names. */,
4240
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
4341
"paths": {
44-
"@uiw/react-native": ["node_modules/@uiw/react-native/src/index"]
42+
"@uiw/react-native": ["node_modules/@uiw/react-native/src/index"],
43+
"@uiw/react-native-image-picker": ["node_modules/@uiw/react-native-image-picker/src/index"]
4544
},
4645
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
4746
// "typeRoots": [], /* List of folders to include type definitions from. */
@@ -61,10 +60,5 @@
6160
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
6261
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
6362
},
64-
"exclude": [
65-
"node_modules",
66-
"babel.config.js",
67-
"metro.config.js",
68-
"jest.config.js"
69-
]
63+
"exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"]
7064
}

packages/react-native-image-picker/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"@uiw/react-native":"~3.2.3",
3232
"react-native-svg": "12.1.1",
3333
"react-native-image-picker":"^5.3.1",
34-
"react-native-image-viewing":"~0.2.2"
34+
"react-native-image-viewing":"~0.2.2",
35+
"@react-native-camera-roll/camera-roll":"~5.3.1"
3536
},
3637
"peerDependencies": {
3738
"react": ">=16.9.0",

packages/react-native-image-picker/src/ImagePicker/README.md

Whitespace-only changes.

packages/react-native-image-picker/src/ImagePicker/image-picker.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useMemo } from 'react';
2-
import { View, StyleSheet, TouchableOpacity } from 'react-native';
2+
import { View, StyleSheet, TouchableOpacity, Alert } from 'react-native';
33
import { Theme, Flex, ActionSheet, ActionSheetItem, TransitionImage, Icon } from '@uiw/react-native';
44
import ImageView from 'react-native-image-viewing';
55
import { useTheme } from '@shopify/restyle';
@@ -59,6 +59,7 @@ const ImagePicker = ({
5959
handlePress,
6060
previewVisible,
6161
setLaunchVisibleFalse,
62+
saveImage,
6263
} = useImagePicker({
6364
value,
6465
options,
@@ -72,6 +73,15 @@ const ImagePicker = ({
7273
libraryRationale,
7374
selectionLimit,
7475
});
76+
const savePhotos = (url = '') => {
77+
Alert.alert('保存图片', '', [
78+
{ text: '取消', style: 'cancel' },
79+
{
80+
text: '确认',
81+
onPress: () => saveImage(url),
82+
},
83+
]);
84+
};
7585

7686
const pictureList = useMemo(() => {
7787
if (showUploadImg && currentImgSource && currentImgSource.length > 0) {
@@ -146,9 +156,11 @@ const ImagePicker = ({
146156
</ActionSheet>
147157
<ImageView
148158
visible={previewVisible}
159+
keyExtractor={(_, index) => index + ''}
149160
onRequestClose={closePreviewImage}
150161
imageIndex={current || 0}
151162
images={previewImages}
163+
onLongPress={(image: any) => savePhotos(image.uri)}
152164
{...others}
153165
/>
154166
</View>

packages/react-native-image-picker/src/ImagePicker/useImagePicker.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { useEffect, useState } from 'react';
2-
import { Keyboard, PermissionsAndroid, Platform } from 'react-native';
2+
import { Alert, Keyboard, PermissionsAndroid, Platform } from 'react-native';
33
import {
44
CameraOptions,
55
ImagePickerResponse,
66
launchImageLibrary,
77
launchCamera as launchRNCamera,
88
} from 'react-native-image-picker';
99
import { useBoolean, useSafeState } from 'ahooks';
10+
import { CameraRoll } from '@react-native-camera-roll/camera-roll';
1011
import type { File, ImagePickerProps } from './types';
1112

1213
function getSource(value?: string) {
@@ -181,6 +182,30 @@ export default function useImagePicker({
181182
setLaunchVisibleTrue();
182183
};
183184

185+
// 保存图片
186+
const saveImage = async (url?: string | undefined) => {
187+
// 检查android权限
188+
if (Platform.OS === 'android') {
189+
const permission =
190+
Platform.Version >= 33
191+
? PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES
192+
: PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE;
193+
const granted = await PermissionsAndroid.request(permission);
194+
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
195+
// 如果权限未获得,进行提示或者打开应用设置页面
196+
return;
197+
}
198+
}
199+
if (url) {
200+
try {
201+
await CameraRoll.save(getSource(url));
202+
Alert.alert('保存成功', '', [{ text: '确定' }]);
203+
} catch (error) {
204+
Alert.alert('保存失败!\n' + error, '', [{ text: '确定' }]);
205+
}
206+
}
207+
};
208+
184209
return {
185210
currentImgSource,
186211
current,
@@ -194,6 +219,6 @@ export default function useImagePicker({
194219
deleteImage,
195220
handlePress,
196221
setLaunchVisibleFalse,
197-
refresh,
222+
saveImage,
198223
};
199224
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
ImagePicker 图片上传
2+
---
3+
上传图片/预览图片/保存图片
4+
5+
### 使用本组件需要单独安装
6+
```bash
7+
yarn add @uiw/react-native-image-picker react-native-image-picker @react-native-camera-roll/camera-roll react-native-image-viewing
8+
```
9+
该组件依赖
10+
- `图片上传` [react-native-image-picker](https://github.com/react-native-image-picker/react-native-image-picker)
11+
- `图片预览` [react-native-image-viewing](https://github.com/jobtoday/react-native-image-viewing)
12+
- `图片保存` [@react-native-camera-roll/camera-roll](https://github.com/react-native-cameraroll/react-native-cameraroll)
13+
14+
### 权限
15+
16+
#### ios
17+
请在`ios/<应用名称>/Info.plist`中添加以下代码
18+
```xml
19+
<key>NSCameraUsageDescription</key>
20+
<string>This app requires access to the camera.</string>
21+
<key>NSPhotoLibraryAddUsageDescription</key>
22+
<string>This app requires access to the photo.</string>
23+
```
24+
25+
#### android
26+
请在`android/app/src/main/AndroidManifest.xml` `<manifest>` 标签下添加以下代码
27+
28+
##### Android 13以下
29+
在 Android 13以下,记得将`android:requestLegacyExternalStorage="true"`属性添加到`<application>`中来支持 Android 10
30+
```xml
31+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
32+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
33+
```
34+
35+
##### Android 13以上
36+
在 Android 13以上,`READ_EXTERNAL_STORAGE已`已经被`READ_MEDIA_IMAGES``READ_MEDIA_VIDEO`取代
37+
```xml
38+
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
39+
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
40+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
41+
```
42+
43+
```xml
44+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
45+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
46+
```
47+

0 commit comments

Comments
 (0)