我有来自Github dzenbot/DZNPhotoPickerController的以下Objective-C块分配示例,我正在努力转换为Swift。 Controller是一个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!'
答案 0 :(得分:0)
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)
}
}
答案 1 :(得分:0)
Objective-C块没有返回任何内容,
controller.finalizationBlock = { (picker: UIImagePickerController, info: NSDictionary) -> (Void) in
var image = UIImagePickerControllerEditedImage(info)
imageView.image = image
// Dismiss when the crop mode was disabled
if (picker.cropMode == .None) {
dismissEditor(picker)
}
}
注意1.如果您打算在其他函数中使用闭包,您可以声明一个Alias以提供更易读的代码(但这不是强制性的)
typealias UIImagePickerControllerFinalizationBlock = (picker: UIImagePickerController, info: NSDictionary) -> (Void)
注意2.也许您可以使用[String: AnyObject]
或类型信息参数使用的更多信息参数类型“ Swifty ”。
答案 2 :(得分:0)
试试这可能会对你有所帮助:
controller.finalizationBlock = {(picker: UIImagePickerController, info: [NSObject : AnyObject]) -> Void in
var image: UIImage = info[UIImagePickerControllerEditedImage]
weakSelf.imageView.image = image
// Dismiss when the crop mode was disabled
if picker.cropMode == DZNPhotoEditorViewControllerCropMode.None
{
weakSelf.dismissController(picker)
}
}