首页 > 编程学习 > Flutter 调用 iOS 相机 回传照片

Flutter 调用 iOS 相机 回传照片

发布时间:2022/2/7 9:38:29

想做个相机调用做个混编总结,然后回传照片给Flutter。

flutter没有UIImage对应的类型,就是说回调时直接回传UIImage时报错,说没有此类型,Flutter和NA互调信息都是走的字节流。然后把UIimage转成NSData类型回传,Flutter接收时拿到图片data,利用Image.memory(value) 获取图片并进行显示即可。

下面直接上代码:

ios端:

    [self.flutterVC setInitialRoute:@"ChoosePicExamplePage"];

    FlutterMethodChannel *methodChannel = [FlutterMethodChannel methodChannelWithName:@"example" binaryMessenger:self.flutterVC.binaryMessenger];

    [methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {

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

            self.methodCannelResultBlock = result; //拿到回调block,回传照片时用

            [self choosePic];//唤起相册,页面已经是flutter页面,所以需要self.flutter present

        }

    }];

    [self presentViewController:self.flutterVC animated:YES completion:nil];

- (void)choosePic{

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    imagePicker.delegate = self;

    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

    [self.flutterVC presentViewController:imagePicker animated:YES completion:nil];

}

#pragma mark imagePickerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info{

    [picker dismissViewControllerAnimated:YES completion:nil];

    UIImage *image = info[UIImagePickerControllerOriginalImage];

    NSData *imageData = UIImagePNGRepresentation(image);

    self.methodCannelResultBlock(imageData);

    }

 

flutter端代码:

只贴关键代码把。。。其他不重要。。

channel.invokeMethod('choosePic').then((value) {_image = Image.memory(value);setState(() {});

then拿方法调用的回调,Image.memory方法把字节流图片转成Image类型,然后刷新界面显示图片

 

为了测试所以全部都是手动的,flutter有插件,不用管原生,直接集成插件就能实现此功能。叫什么imagepick啥的。没记住。。


本文链接:https://www.ngui.cc/51cto/show-397.html
Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000