【问题标题】:Flutter pass Uint8List to iOS Objective-cFlutter 将 Uint8List 传递给 iOS Objective-c
【发布时间】:2020-03-10 23:25:01
【问题描述】:

我正在尝试将图像作为 Uint8List 从 Flutter 传递给原生代码 Android/iOS,但在 iOS 端出现错误。我正在修改一个插件,我以前从未在 iOS 中开发过。 这是从小部件中捕获图像并将 Uint8List 发送到本机代码的 Flutter 代码。

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

在 Android 中,我使用的是 Kotlin:

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

在 iOS 中,插件是用 Objective-C 编写的。我为我的图片添加了此检查

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

     NSData *imgBytes = call.arguments;

     UIImage *label = [UIImage imageWithData:imgBytes];

 }

我在 iOS 中看到 Uint8ListFlutterStandardTypedData typedDataWithBytes,但是当我将 imgBytes 类型设置为它时出现错误。

【问题讨论】:

    标签: android ios objective-c flutter


    【解决方案1】:

    您应该将第一个参数作为类型数据,可能:

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

    科特林:

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

    然后你像这样调用它:

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

    【讨论】:

    • 谢谢,试试看
    【解决方案2】:

    我刚刚处理了这个。让Xcode 进行神奇的转换让我感到很痛苦。例如,在您的 iOS 插件实现中,行:

    NSData *imgBytes = call.arguments;
    

    当我这样做时,在Xcode 中,调试器会看到数据类型将以FlutterStandardTypedData 的形式出现(因为我传入了一个整数列表)但每次我都会尝试访问一个元素/对象它会给我记忆错误。就像Xcode 在现实中尊重我的参数列表(NSArray),但在调试时将数据类型显示为FlutterStandardTypedData。老实说,这似乎是IDE 中的一个错误。

    正如赞成的答案所示,我通过使用颤振数据类型解决了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-13
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      相关资源
      最近更新 更多