首页 > 解决方案 > 与 Expo 配合使用的 React Native 图像裁剪工具

问题描述

有谁知道在 Expo 设置中工作的 React Native 的图像裁剪工具。非常流行的react-native-image-crop-picker没有。有其他选择吗?我似乎找不到任何东西。

标签: react-nativeexpo

解决方案


您可以使用下载图像,Expo#FileSystem然后使用Expo#ImageManipulator 以下示例裁剪缓存的图像

/*
 * @param link {string} URI of the image on the server
 * @param name {string} Name of the image with extension
 */
_downloadAndCrop = (link, name, cropSize = { width: 200, height: 200 }) => {
    FileSystem.downloadAsync(
       link,
       name
   )
   .then(({ uri }) => {
       console.log('Finished downloading to ', uri);
       //Your new cropped image
       const cropImage = ImageManipulator.manipulate(uri, [
                             crop: { 
                                 originX: 0, 
                                 originY: 0, 
                                 width: cropSize.width, 
                                 height: cropSize.height 
                             }
                         }], {});
   })
   .catch(error => {
       console.error(error);
   });
}

推荐阅读