美文网首页
react-native选择图片上传报错 Error Domai

react-native选择图片上传报错 Error Domai

作者: 朱传武 | 来源:发表于2021-03-13 10:30 被阅读0次
image.png

解决办法:
1、修改react-native库

 -(id)sendRequest:(NSURLRequest *)request withDelegate: that exists in node_modules/react-native/Libraries/Image/RCTImageLoader.mm

修改成如下内容:

- (id)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate>)delegate
{
    @synchronized(self) {
       __block RCTImageLoaderCancellationBlock requestToken = ^{};
       requestToken = [self loadImageWithURLRequest:request callback:^(NSError *error, UIImage *image) {
         @synchronized(self) {

           if (error) {
             [delegate URLRequest:requestToken didCompleteWithError:error];
             return;
           }


        NSString *mimeType = nil;
        NSData *imageData = nil;
        if (RCTImageHasAlpha(image.CGImage)) {
          mimeType = @"image/png";
          imageData = UIImagePNGRepresentation(image);
        } else {
          mimeType = @"image/jpeg";
          imageData = UIImageJPEGRepresentation(image, 1.0);
        }
   NSURLResponse *response = [[NSURLResponse alloc] initWithURL:request.URL
                                                            MIMEType:mimeType
                                               expectedContentLength:imageData.length
                                                    textEncodingName:nil];


             [delegate URLRequest:requestToken didReceiveResponse:response];
                   [delegate URLRequest:requestToken didReceiveData:imageData];
                   [delegate URLRequest:requestToken didCompleteWithError:nil];
                 }
               }];

      return requestToken;
  }
}

2、安装修复工具:patch-package:

yarn add patch-package postinstall-postinstall
or
npm i patch-package

3、添加脚本:

  "scripts": {
     ...
    "postinstall": "patch-package"

4、运行脚本进行修复:

npx patch-package react-native
image.png

5、运行pod install 、react-native run-ios

相关文章

网友评论

      本文标题:react-native选择图片上传报错 Error Domai

      本文链接:https://www.haomeiwen.com/subject/spqxcltx.html