简体   繁体   English

Flutter 将 Uint8List 传递给 iOS Objective-c

[英]Flutter pass Uint8List to iOS Objective-c

I am trying to pass an image as Uint8List to native code Android/iOS from Flutter, but getting an error in iOS side.我正在尝试将图像作为 Uint8List 从 Flutter 传递给本机代码 Android/iOS,但在 iOS 端出现错误。 I am modifying a plugin and i never developed in iOS before.我正在修改一个插件,我之前从未在 iOS 中开发过。 Here is the Flutter code to capture an image from widget and send the Uint8List to native code.这是 Flutter 代码,用于从小部件捕获图像并将 Uint8List 发送到本机代码。

  Future<void> _capturePng() async {
    RenderRepaintBoundary boundary =
    globalKey.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage(pixelRatio: 1.50);
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();

    printer.printImage(pngBytes);
  }

In Android, i am using Kotlin:在 Android 中,我使用的是 Kotlin:

private fun printImage(call: MethodCall, result: Result) {
    val image = call.arguments as ByteArray
    val res = mPrinterPlugin.printImage(image)
    result.success(res)
}

In iOS, the plugin is written in Objective-C.在iOS中,插件写在Objective-C中。 i added this check for my image我为我的图片添加了此检查

else if([@"imagePrint" isEqualToString:call.method]){

     NSData *imgBytes = call.arguments;

     UIImage *label = [UIImage imageWithData:imgBytes];

 }

I saw that Uint8List is FlutterStandardTypedData typedDataWithBytes in iOS, but when i set the imgBytes type to it i get an error.我在 iOS 中看到Uint8ListFlutterStandardTypedData typedDataWithBytes typedDataWithBytes,但是当我将 imgBytes 类型设置为它时出现错误。

You should take first argument as a typed data, probably:您应该将第一个参数作为类型数据,可能是:

NSArray *args = call.arguments;
FlutterStandardTypedData *list = args[0];

kotlin: kotlin:

val args: List<Any> = call.arguments as List<Any>
val list = args[0] as ByteArray

and you invoke it like this:你像这样调用它:

Uint8List uint8List;
_channel.invokeMethod("your_function_name", [uint8List]);

I just dealt with this.我刚刚处理了这个。 Letting Xcode do its magical conversion was burning me.Xcode做它的神奇转换让我很着急。 For instance, in your iOS plugin implementation, the line:例如,在您的 iOS 插件实现中,该行:

NSData *imgBytes = call.arguments;

When i was doing this, in Xcode the debugger would see the data type would come through as a FlutterStandardTypedData (as i passed in a list of ints) but every time i would try to access an element/object it would give me memory errors.当我这样做时,在Xcode中,调试器会看到数据类型将以FlutterStandardTypedData的形式出现(因为我传入了一个整数列表),但每次我尝试访问一个元素/对象时,它都会给我 memory 错误。 It was like Xcode was honoring my parameter list ( NSArray ) in reality, but showing me the data type as a FlutterStandardTypedData while debugging.就像Xcode在现实中尊重我的参数列表( NSArray ),但在调试时向我显示数据类型为FlutterStandardTypedData Seemed like a bug in the IDE to be honest.老实说,这似乎是IDE中的一个错误。

Just as the upvoted answer shows, i resolved by using the flutter data type:正如赞成的答案所示,我通过使用 flutter 数据类型解决了问题:

FlutterStandardTypedData *bytesList = call.arguments[@"listOfBytes"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM