我将以react-native方式旋转图像,我将获得旋转图像的base64。 我用了几个库
react-native-image-rotate
:在Android上运行良好,但在iOS上我获得了rct-image-store://1
作为网址,因此我尝试使用rn-fetch-blob
来获取base64,但这引发了无法执行的错误识别该网址。
react-native-image-resizer
:我使用了此功能,但在iOS中响应不佳。如果我将-90设置为-180,则将其旋转为-270。
请帮助我解决这个问题,如何在iOS中旋转图像。
我需要将图像旋转为-90,-180,-270,-360(原始)。
答案 0 :(得分:2)
最后,我找到了答案。
import ImageRotate from 'react-native-image-rotate';
import ImageResizer from 'react-native-image-resizer';
import RNFetchBlob from 'rn-fetch-blob';
ImageRotate.rotateImage(
this.state.image.uri,
rotateDegree,
uri => {
if (Platform.OS === 'android') {
console.log('rotate', uri);
RNFetchBlob.fs.readFile(uri, 'base64').then(data => {
const object = {};
object.base64 = data;
object.width = this.state.image.height;
object.height = this.state.image.width;
object.uri = uri;
this.setState({image: object, spinner: false});
});
} else {
console.log(uri);
const outputPath = `${RNFetchBlob.fs.dirs.DocumentDir}`;
ImageResizer.createResizedImage(
uri,
this.state.image.height,
this.state.image.width,
'JPEG',
100,
0,
outputPath,
).then(response => {
console.log(response.uri, response.size);
let imageUri = response.uri;
if (Platform.OS === 'ios') {
imageUri = imageUri.split('file://')[1];
}
RNFetchBlob.fs.readFile(imageUri, 'base64').then(resData => {
const object = {};
object.base64 = resData;
object.width = this.state.image.height;
object.height = this.state.image.width;
object.uri = response.uri;
this.setState({image: object, spinner: false});
});
});
}
},
error => {
console.error(error);
},
);
}
答案 1 :(得分:0)
这是到目前为止我的工作良好的代码
rotateImage = (angle) => {
const { currentImage } = this.state; // origin Image, you can pass it from params,... as you wish
ImageRotate.rotateImage( // using 'react-native-image-rotate'
currentImage.uri,
angle,
(rotatedUri) => {
if (Platform.OS === 'ios') {
ImageStore.getBase64ForTag( // import from react-native
rotatedUri,
(base64Image) => {
const imagePath = `${RNFS.DocumentDirectoryPath}/${new Date().getTime()}.jpg`;
RNFS.writeFile(imagePath, `${base64Image}`, 'base64') // using 'react-native-fs'
.then(() => {
// now your file path is imagePath (which is a real path)
if (success) {
this.updateCurrentImage(imagePath, currentImage.height, currentImage.width);
ImageStore.removeImageForTag(rotatedUri);
}
})
.catch(() => {});
},
() => {},
);
} else {
this.updateCurrentImage(rotatedUri, currentImage.height, currentImage.width);
}
},
(error) => {
console.error(error);
},
);
};
我认为您已经完成了rotatedUri
。
ImageStore
的{{1}}获取base64。 react-native
之后,您拥有react-native-fs
是本地图像。
答案 2 :(得分:0)
尝试使用 Expo Image Manipulator https://docs.expo.io/versions/latest/sdk/imagemanipulator/
const rotated = await ImageManipulator.manipulateAsync(
image.uri,
[{ rotate: -90 }],
{ base64: true }
);