簡體   English   中英

Flutter 將 Uint8List 傳遞給 iOS Objective-c

[英]Flutter pass Uint8List to iOS Objective-c

我正在嘗試將圖像作為 Uint8List 從 Flutter 傳遞給本機代碼 Android/iOS,但在 iOS 端出現錯誤。 我正在修改一個插件,我之前從未在 iOS 中開發過。 這是 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);
  }

在 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 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]);

我剛剛處理了這個。 Xcode做它的神奇轉換讓我很着急。 例如,在您的 iOS 插件實現中,該行:

NSData *imgBytes = call.arguments;

當我這樣做時,在Xcode中,調試器會看到數據類型將以FlutterStandardTypedData的形式出現(因為我傳入了一個整數列表),但每次我嘗試訪問一個元素/對象時,它都會給我 memory 錯誤。 就像Xcode在現實中尊重我的參數列表( NSArray ),但在調試時向我顯示數據類型為FlutterStandardTypedData 老實說,這似乎是IDE中的一個錯誤。

正如贊成的答案所示,我通過使用 flutter 數據類型解決了問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM