微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在iOS上使用react-native-image-picker

如何解决无法在iOS上使用react-native-image-picker

我正在使用react-native-image-picker选择并上传服务器上的图像。它在android上运行良好,但在iOS上引发错误。 npm link提供了一个注释,即“在iOS上,不要假设返回的绝对uri将持续存在。请参阅#107”是什么意思?这个程式库可以在iOS上运作吗?我正在尝试使用iOS模拟器,它可以正确显示图像,但是当我尝试使用axios在服务器上上传时,它会引发错误“缺少请求的请求令牌: {URL:file://”,例如{ {3}} 是否还有其他替代的react-native库可以使用iOS和android上传图像?

解决方法

如果您尝试将文件上传到服务器,则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,});
}
,

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

转到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);
   };
 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。