React Native v0.71.0(非Expo)图片旋转替代库求推荐
替代react-native-image-rotate的图片旋转方案(React Native 0.71.0 非Expo环境)
方案1:@react-native-community/image-editor
- 维护状态稳定,支持基础图片旋转、裁剪操作,与RN 0.71版本兼容性良好
- 核心旋转代码示例:
import ImageEditor from '@react-native-community/image-editor'; const rotateImage = async (imageUri, degrees) => { try { const result = await ImageEditor.cropImage( imageUri, { offset: { x: 0, y: 0 }, size: { width: 0, height: 0 }, // 0表示保留原图尺寸 displaySize: { width: 0, height: 0 }, resizeMode: 'contain', rotate: degrees // 传入旋转角度,如90、180、270 } ); return result; } catch (error) { console.error('图片旋转失败:', error); } };
- 安装后执行
npx pod-install完成iOS依赖配置,Android无需额外操作,构建前建议执行npm run clean清理缓存
方案2:@shopify/react-native-skia
- 基于Skia图形库,除旋转外还支持复杂图片编辑,适配RN 0.71无压力
- 通过Canvas绘制实现旋转,示例代码:
import { Canvas, Image, useImage } from '@shopify/react-native-skia'; const RotatedImage = ({ uri, degrees }) => { const image = useImage(uri); if (!image) return null; const radians = (degrees * Math.PI) / 180; return ( <Canvas style={{ width: image.width, height: image.height }}> <Image image={image} x={0} y={0} width={image.width} height={image.height} transform={[{ rotate: radians }]} /> </Canvas> ); };
- 安装需严格遵循官方配置步骤,iOS需确认Skia的pod源配置正确,Android检查gradle依赖版本匹配
方案3:原生桥接手动实现
- 若第三方库均出现构建问题,可编写少量原生代码实现旋转,完全可控无兼容性风险
- Android端示例(Java模块):
@ReactMethod public void rotateImage(String imagePath, int degrees, Promise promise) { try { Bitmap bitmap = BitmapFactory.decodeFile(imagePath); Matrix matrix = new Matrix(); matrix.postRotate(degrees); Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); String outputPath = getCacheDir() + "/rotated_img.jpg"; rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, new FileOutputStream(outputPath)); promise.resolve(outputPath); } catch (Exception e) { promise.reject("ROTATE_ERROR", e.getMessage()); } }
- iOS端可通过
UIImage的分类方法实现旋转,再通过RN桥接暴露给JS调用
内容的提问来源于stack exchange,提问作者Firas Bouadila
相关产品推荐
相关产品推荐

