如何将Objective-C块转换为Swift闭包?

格莱美

我有下面的Github dzenbot / DZNPhotoPickerController的Objective-C块分配示例,我正努力将其转换为Swift。控制器是一个UIImagePickerController。

controller.finalizationBlock = ^(UIImagePickerController *picker, NSDictionary *info) {

    UIImage *image = info[UIImagePickerControllerEditedImage];
    weakSelf.imageView.image = image;

    // Dismiss when the crop mode was disabled
    if (picker.cropMode == DZNPhotoEditorViewControllerCropModeNone) {
        [weakSelf dismissController:picker];
    }
};

我已经尝试了以下方法,但是无法弄清楚我要去哪里。

controller.finalizationBlock = { (picker: UIImagePickerController, info: NSDictionary) -> UIImagePickerControllerFinalizationBlock! in
        var image = UIImagePickerControllerEditedImage(info)
        imageView.image = image

        // Dismiss when the crop mode was disabled
        if (picker.cropMode == .None) {
            dismissEditor(picker)
        }
    }

我得到的错误是:

“无法分配类型'(UIImagePickerController,NSDictionary)-> UIImagePickerControllerFinalizationBlock的值!键入“ UIImagePickerControllerFinalizationBlock!”

泽蒙

Objective-C块不返回任何内容,因此对它的关闭应如下所示:

controller.finalizationBlock = { (picker, info) in
        var image = UIImagePickerControllerEditedImage(info)
        imageView.image = image

        // Dismiss when the crop mode was disabled
        if (picker.cropMode == .None) {
            dismissEditor(picker)
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章