/
saveViewToCamera.js
64 lines (53 loc) · 1.57 KB
/
saveViewToCamera.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import ViewShot from 'react-native-view-shot'
import React, { Component } from 'react'
import { View, CameraRoll, Platform, PermissionsAndroid } from 'react-native'
import { requestAndroidPermission } from './permissions'
import I18n from '../locale'
export class ViewExportComponent extends Component {
async componentDidMount () {
const resolve = this.props.navigation.getParam('resolve')
const reject = this.props.navigation.getParam('reject')
try {
const uri = await this.refs.viewShot.capture({ format: 'jpg' })
resolve(uri)
} catch (e) {
reject(e)
}
this.props.navigation.goBack(null)
}
render () {
const view = this.props.navigation.getParam('view')
return (
<View style={{ opacity: 0 }}>
<ViewShot ref='viewShot'>{view}</ViewShot>
</View>
)
}
}
export default async ({ view, navigation }) => {
if (Platform.OS === 'android') {
const allowed = await requestAndroidPermission({
permission: PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
title: I18n.t('settings.save-picture-perm'),
message: I18n.t('settings.save-picture-perm-desc'),
})
if (!allowed) {
throw new Error(I18n.t('settings.save-picture-perm-denied'))
}
}
if (Platform.OS === 'web') {
throw new Error('unsupported on web')
}
try {
const uri = await new Promise((resolve, reject) => {
navigation.navigate('virtual/view-export', {
resolve,
reject,
view,
})
})
await CameraRoll.saveToCameraRoll(uri, 'photo')
} catch (e) {
throw e
}
}