如何将图像数据传递给React Native中的本机模块?

问题描述 投票:2回答:2

在我在Xcode中实现RCTBridgeModule协议的类中,我正在尝试编写一个RCT_EXPORT_MEATHOD,我可以将其暴露给React Native代码以使用图像数据。目前,我可以在React Native中将映像写入磁盘,然后将路径传递给本机方法,但我想知道是否有更好的技术可以直接传递图像数据以获得更好的性能?

所以不是这样的:

RCT_EXPORT_METHOD(scanImage:(NSString *)path) {
  UIImage *sampleImage = [[UIImage alloc] initWithContentsOfFile:path];  
  [self processImage: UIImage];
}

更像这样的东西:

RCT_EXPORT_METHOD(scanImage:(NSData *)imageData) {
  [self processImageWithData: imageData];
}
react-native uiimage nsdata native-methods native-module
2个回答
0
投票

你可以使用[RCTConvert UIImage:icon]#import <React/RCTConvert.h>方法

您需要将“schema”指定为“data”。有关更多详细信息,请查看以下源代码:https://github.com/facebook/react-native/blob/master/React/Base/RCTConvert.m#L762


0
投票

对我来说,这有效:

  NSURL *url = [NSURL URLWithString:[imagePath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
  UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];

imagePathNSString来自反应原生的桥梁(使用react-native-image-picker的回应 - response.uri)。

© www.soinside.com 2019 - 2024. All rights reserved.