【问题标题】:Unable to use react-native-image-picker on iOS无法在 iOS 上使用 react-native-image-picker
【发布时间】:2020-09-20 08:06:50
【问题描述】:

我正在使用 react-native-image-picker 在服务器上选择和上传图片。它在 android 上运行良好,但在 iOS 上抛出错误。 npm link 提供了一条注释“在 iOS 上,不要假设返回的绝对 uri 会持续存在。请参阅 #107”这是什么意思?这个库可以在 iOS 上运行吗?我正在尝试使用 iOS 模拟器,它可以正确显示图像,但是当我尝试使用 axios 在服务器上上传时,它会抛出错误“Missing request token for request: { URL: file://” like @ 987654322@ 有没有其他的 react-native 库可以用 iOS 和 android 上传图片?

【问题讨论】:

  • 感谢 anthony willis muñoz 指出这一点。在挣扎了 16 小时后,我发现 react-native-image-picker 和 react-native-image-crop-picker 都不能在 iOS 模拟器上使用。它在真实设备上运行良好。

标签: ios react-native react-native-image-picker


【解决方案1】:

这是一个内部错误。试试看。

转到node_modules/react-native/Image/RCTLocalAssetImageLoader.mm 并将此行更改为:

 - (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
                                           size:(CGSize)size
                                          scale:(CGFloat)scale
                                     resizeMode:(RCTResizeMode)resizeMode
                                progressHandler:(RCTImageLoaderProgressBlock)progressHandler
                             partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
                              completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
 {
   __block auto cancelled = std::make_shared<std::atomic<bool>>(false);
   RCTExecuteOnMainQueue(^{
     if (cancelled->load()) {
       return;
     }

     UIImage *image = RCTImageFromLocalAssetURL(imageURL);
     if (image) {
       if (progressHandler) {
         progressHandler(1, 1);
       }
       completionHandler(nil, image);
     } else {
       NSString *message = [NSString stringWithFormat:@"Could not find image %@", imageURL];
       RCTLogWarn(@"%@", message);
       completionHandler(RCTErrorWithMessage(message), nil);
     }
   });

   return ^{
     cancelled->store(true);
   };
 }

【讨论】:

  • 感谢莱昂纳多·莱特。我已经检查了该文件,似乎最新的更新已经在包本身中进行了此更改。
  • @Leonardo 文件在该位置找不到 node_modules/react-native/Image/RCTLocalAssetImageLoader.mm
  • 嘿@PunnetKansal 在 node_modules/react-native/Libraries/Image/RCTLocalAssetImageLoader.mm 中尝试一下
  • 我应该用这段代码替换所有代码吗?
【解决方案2】:

如果您尝试将文件上传到服务器,react-native-image-pickerreact-native-image-crop-picker 将无法在 iOS 模拟器上运行。它会抛出:

缺少请求的请求令牌: { URL: file://' 错误。

它在 android 和 ios 的真实设备上运行良好。只需使用这样的文件名。

ImagePicker.openPicker({
  width: 300,
  height: 400,
  cropping: true,
  compressImageMaxHeight: 300,
  compressImageMaxWidth: 400,
  compressImageQuality: 0.5,
  mediaType: 'photo',
}).then(image => {
 
let filename = image.path.split('/').pop();
  
  let formData = new FormData();

  formData.append('file', {
    name: Platform.OS === 'android' ? filename : image.filename,
    type: image.mime,
    uri: image.path, 
  });
}

【讨论】:

    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 2019-05-29
    • 2021-10-20
    • 2020-03-13
    • 2021-09-28
    • 2021-05-11
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多