Kev*_*dez 1 compression camera media-library react-native expo
我正在使用 expo 和 MediaLibrary 中的相机组件来保存拍摄的照片。我的问题是,当使用图库中的 MediaLibrary 保存该图像时,如何压缩该图像?我正在尝试压缩它,因为稍后我还将将该图像上传到 Firebase 存储。到目前为止,这是我现在没有压缩的代码:
import React, { useState, useEffect} from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { Camera } from 'expo-camera';
import * as MediaLibrary from 'expo-media-library';
import { Dimensions } from 'react-native';
const {height, width} = Dimensions.get('window');
export default function App() {
const [hasPermission, setHasPermission] = useState(null);
const [cameraRef, setCameraRef] = useState(null)
const [type, setType] = useState(Camera.Constants.Type.back);
useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
MediaLibrary.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={{ flex: 1 }}>
<Camera style={{ flex: 1 }} type={type} ref={ref => {
setCameraRef(ref) ;
}}>
<View
style={{
flex: 1,
backgroundColor: 'transparent',
justifyContent: 'flex-end'
}}>
<TouchableOpacity style={{alignSelf: 'center', marginBottom: 20}} onPress={async() => {
if(cameraRef){
let photo = await cameraRef.takePictureAsync({ skipProcessing: true });
MediaLibrary.saveToLibraryAsync(photo.uri);
}
}}>
<View style={{
borderWidth: 2,
borderColor: 'white',
height: 50,
width:50,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 25,
}}
>
<View style={{
borderWidth: 2,
borderColor: 'white',
height: 40,
width:40,
backgroundColor: 'white',
borderRadius: 25}} >
</View>
</View>
</TouchableOpacity>
</View>
</Camera>
</View>
);
}
Run Code Online (Sandbox Code Playgroud)
ImageManipulator 附带 expo,您可以压缩、调整大小、旋转、裁剪等等。
import { ImageManipulator } from 'expo';
const manipResult = await ImageManipulator.manipulate(
imageUri,
[{ resize: { width: 640, height: 480 } }],
{ format: 'jpg' }
);
Run Code Online (Sandbox Code Playgroud)
查看https://docs.expo.io/versions/latest/sdk/imagemanipulator/
| 归档时间: |
|
| 查看次数: |
11848 次 |
| 最近记录: |