android - Flutter 将 Uint8List 传递给 iOS Objective-c
问题描述
我正在尝试将图像作为 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];
}
我看到这Uint8List
是FlutterStandardTypedData typedDataWithBytes
在 iOS 中,但是当我将 imgBytes 类型设置为它时,我得到了一个错误。
解决方案
您应该将第一个参数作为类型数据,可能是:
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]);
推荐阅读
- mysql - 如何将for循环的输出格式更改为字符串而不是列表?
- unity3d - 如何制作一个像地球一样的地形?
- python - 使用 PDFMiner 时出现 OSError “不是有效的 Win32 应用程序”
- excel - 上个月召回格式
- javascript - 无限循环 GoogleAppScript
- python - 为什么当代码相同时我的输出不同?
- applescript - 如何识别字符串中的 6 个连续数字?
- javascript - React Router V4,不匹配Routes中第一个父级下的任何内容
- powershell - 在 Powershell 中将两个数组输出转换为具有动态更新的表
- google-apps-script - Google Appmaker 中的 JDBC 连接 - 不想在脚本中存储密码