// Grab reference to avoid retain on self. // `self` might be released before the block, so the block needs to retain the codec to // make sure it is not released with `self` /// 从前面的代码可以知道这个self即channel对象只有一个局部对象在持有,所以超过作用域会被回收,所以这里接收到codec NSObject<FlutterMessageCodec>* codec = _codec; FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReply callback) { handler([codec decode:message], ^(id reply) { callback([codec encode:reply]); }); }; _connection = SetMessageHandler(_messenger, _name, messageHandler, _taskQueue); }
- (FlutterBinaryMessengerConnection) setMessageHandlerOnChannel:(NSString*)channel binaryMessageHandler:(FlutterBinaryMessageHandler)handler taskQueue:(NSObject<FlutterTaskQueue>* _Nullable)taskQueue { NSParameterAssert(channel); if (_shell && _shell->IsSetup()) { /// 获取原生平台的线程,并传入channel名,回调,任务队列 self.platformView->GetPlatformMessageHandlerIos()->SetMessageHandler(channel.UTF8String, handler, taskQueue); /// std::unique_ptr<flutter::ConnectionCollection> _connections; /// /// 文件:connection_collection.mm /// ConnectionCollection::Connection ConnectionCollection::AquireConnection(const std::string& name) { /// Connection nextConnection = ++counter_; /// connections_[name] = nextConnection; /// return nextConnection; /// } /// FlutterEngine对象中的连接集合属性,AcquireConnection方法让connections字典中key为channel的计数加1 return _connections->AquireConnection(channel.UTF8String); } else { NSAssert(!handler, @"Setting a message handler before the FlutterEngine has been run."); // Setting a handler to nil for a channel that has not yet been set up is a no-op. return flutter::ConnectionCollection::MakeErrorConnection(-1); } }
文件: platform_view_ios.h
1 2 3 4 5 6 7
/// 调用GetPlatformMessageHandlerIos即返回platform_message_handler_属性 class PlatformViewIOS final : public PlatformView { ... std::shared_ptr<PlatformMessageHandlerIos> GetPlatformMessageHandlerIos() const { return platform_message_handler_; } ...
/// Dart端调用获取可用摄像头列表 final cameras = await availableCameras();
文件: camera_controller.dart
1 2 3 4 5 6
/// Completes with a list of available cameras. /// /// May throw a [CameraException]. Future<List<CameraDescription>> availableCameras() async { return CameraPlatform.instance.availableCameras(); }
调用CameraPlatform对象的availableCameras方法
文件: camera_platform.dart
1 2 3 4 5 6 7 8 9 10
abstractclassCameraPlatformextendsPlatformInterface{ /// Constructs a CameraPlatform. CameraPlatform() : super(token: _token); ... /// Completes with a list of available cameras. /// /// This method returns an empty list when no cameras are available. Future<List<CameraDescription>> availableCameras() { throw UnimplementedError('availableCameras() is not implemented.'); }
dartPluginClass: Optional. The Dart class that serves as the entry point for a Flutter plugin. This can be used with the Android, iOS, Linux macOS, and Windows platforms.
await pigeonVar_channel.send(null) as List<Object?>?;
1 2 3 4 5 6 7 8
/// Sends the specified [message] to the platform plugins on this channel. /// /// Returns a [Future] which completes to the received response, which may /// be null. Future<T?> send(T message) async { /// 调用binaryMessenger对象编码发送消息 && 得到返回后再解码 return codec.decodeMessage(await binaryMessenger.send(name, codec.encodeMessage(message))); }
var discoveryDevices: [AVCaptureDevice.DeviceType] = [ .builtInWideAngleCamera, .builtInTelephotoCamera, ] ... /// 前置,后置等摄像头,然后统一添加到reply这个数组对象 for device in devices { var lensFacing: FCPPlatformCameraLensDirection
switch device.position { case .back: lensFacing = .back case .front: lensFacing = .front case .unspecified: ... } reply.append(cameraDescription) } /// 最后执行callback(wrapResult(output, error)); /// 获取成功后,Flutter的Dart部分可以获取到可用的摄像头列表 completion(reply, nil) }