捕获视图并保存到设备- React Native

ggazkfy8  于 2022-11-30  发布在  React
关注(0)|答案(4)|浏览(119)

我正在开发一个应用程序,其中有一个按钮(TouchableHighlight),当按下此按钮时,需要捕获当前屏幕的屏幕快照并将文件保存在设备中。
我的代码没有显示错误,但当我按下按钮(TouchableHighlight)时,我收到消息:
图像已保存到文件:///data/user/0/com.appcamerav4/cache/reactnative-snapshot-image8525057299267209213.jpg***通过远程JS调试***实现。
我无法打开此目录,需要将图像保存到设备。
我是第一次接触React原住民。
下面是我的代码:

import React, { Component } from 'react';
import { Text, View, Image, StyleSheet, TouchableHighlight, WebView, StatusBar, Button } from 'react-native';
import { captureScreen } from "react-native-view-shot";

const zooMais = require('../imgs/zooMais.png');
const zooMenos = require('../imgs/zooMenos.png');
const imgScreeshot = require('../imgs/screeshot.png');


const btnZooMais = ()=>{
    alert("Zoo Mais");
    console.log("Zoom +");
  }
const btnZooMenos = ()=>{
    alert("Zoo Menos");
    console.log("Zoom +");
  }
const capitureScreen = ()=>{
    
    captureScreen({
        format: "jpg",
        quality: 0.8,
        }).then(
        uri => console.log("Image saved to", uri),
        error => console.error("Oops, snapshot failed", error)
        );
}
     
export default class Monitor extends Component {
    
    render() {
        return (  
          
            <View style={ style.viewPrincipal }>

                <StatusBar hidden />

                <View style={ style.viewImagem }  >                
                    <WebView
                        style={style.video}
                        automaticallyAdjustContentInsets={true}
                        scalesPageToFit={true}
                        startInLoadingState={false}
                        contentInset={{top: 0, right: 0, left: 0, bottom: 0}}
                        scrollEnabled={true}
                        source={{uri: 'https://facebook.github.io/react/logo-og.png'}} 
                        onNavigationStateChange = {this.handleNavigationStateChange}
                        />             
                </View>   
                
                <View style={ style.viewRodape }> 
                
                    <View style={style.viewMenu}>
                        <View >
                            <TouchableHighlight onPress={  btnZooMais }   >
                                <Image style={style.imgMenu} source={zooMais } />
                            </TouchableHighlight>
                        </View>  

                        <View>
                            <TouchableHighlight onPress={ capitureScreen }>
                                <Image style={style.imgMenu} source={ imgScreeshot } />
                            </TouchableHighlight >
                        </View>

                        <View>
                            <TouchableHighlight onPress={ btnZooMenos } >
                                <Image style={style.imgMenu} source={ zooMenos } />
                            </TouchableHighlight>
                        </View>

                    </View>
                </View>
            </View>
      
        
        );
        
    }
}

const style = StyleSheet.create({
    viewPrincipal:{
        flex: 1    
    },
    viewImagem:{        
        flex:10,  
        justifyContent:'center',
        alignItems:'stretch'        
    },
    viewRodape:{   
    flex:1.3   
    },
    viewMenu:{
        flexDirection:'row',
        justifyContent: 'space-between'
    },  
    imgMenu:{
        margin: 0,
        marginBottom:0     
      },   
    video:{
        flex:1
        }
});
n8ghc7c1

n8ghc7c11#

若要将屏幕截图保存到相机胶卷,请使用以下方法:https://facebook.github.io/react-native/docs/cameraroll.html#savetocameraroll
更多信息:https://github.com/gre/react-native-view-shot查看常见问题部分

fzsnzjdm

fzsnzjdm2#

确保react-native-view-shot在XCode中正确链接(可能需要手动安装,refer to React Native doc)。

cgfeq70w

cgfeq70w3#

import React, { useRef } from "react"; // import useRef hook on top

const cardRef = useRef(); // Use this hook inside your func. component *important

// Define a function like this
const saveAsImage = async () => {
    try {
      const result = await captureRef(cardRef, {
        result: "tmpfile",
        quality: 1,
        format: "png",
      });
      MediaLibrary.saveToLibraryAsync(result);
    } catch (e) {
      console.log(e);
    }
  };

对组件应用一个属性,例如parentRef={cardRef},确保引用名称匹配,在本例中为“cardRef”。
为任何按钮/可触摸项设置不透明度

onPress={() => {saveAsImage();}}
ajsxfq5m

ajsxfq5m4#

要解决这个问题,你必须去你的应用程序权限在你真正的手机上,并允许相机存储,然后你可以很容易地保存你的ViewShot在你的手机上。

  • go to App Permisssion in your App.info
  • 允许摄像头访问存储

相关问题