android - Flutter 将 Uint8List 传递给 iOS Objective-c

标签 android ios objective-c flutter

我试图将图像作为 Uint8List 从 Flutter 传递到 native 代码 Android/iOS,但在 iOS 端出现错误。我正在修改一个插件,我以前从未在iOS中开发过。 以下是从 widget 捕获图像并将 Uint8List 发送到 native 代码的 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 类型设置为它时,出现错误。

最佳答案

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

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

Kotlin :

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

然后你像这样调用它:

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

关于android - Flutter 将 Uint8List 传递给 iOS Objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58866332/

相关文章:

android - 使用 AlarmManager 手动更新小部件

android - 为什么 item.getMenuInfo() 为空?

ios - 更改 map 图钉(颜色或图像)

ios - MapView 注释中的较长字幕(swift)

android - 使 READ、WRITE 和 NOTIFY 在 Android BLE(版本 21 及更高版本)上工作

iphone - 如何使用新数据重新加载或刷新选项卡栏 Controller 的选项卡栏中的数据?

ios - UITableView:最后一个单元格不完全可见

ios - iPad OS 是否可以检测键盘是否处于 float 模式?

ios - NSNotification 和控制流

php - @Part List< MultipartBody.Part> 在 php 中