我有一个图像裁剪功能,使用委托方法返回裁剪后的图像和CGRect。如何在另一个函数中的自定义完成块中返回它?
有没有办法引用该块,以便我可以在另一个函数中使用它?
很难解释,但这是我的代码:
- (void)cropImage:(UIImage *)image type:(NSInteger)type target:(id)target complete:(cropComplete)complete {
CGFloat ratio;
switch (type) {
case 1:
//16:9
ratio = 16/9.0;
break;
case 2:
//4:3
ratio = 4/3.0;
break;
case 3:
//1:1
ratio = 1;
break;
default:
break;
}
ImageCropViewController *vc = [ImageCropViewController new];
vc.delegate = self;
vc.imageToCrop = image;
vc.ratio = ratio;
UIViewController *targetVC = (UIViewController *)target;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[targetVC presentViewController:nav animated:YES completion:nil];
}
//this is the delegate from ImageCropViewController above
- (void)doneCropping:(UIImage *)croppedImage rect:(CGRect)rect {
(I want the image and CGRect here to return in the ^cropComplete block above)
}
答案 0 :(得分:2)
将稍后要调用的块类型的新属性(^(cropComplete)
)添加到您的班级。
在cropImage:type:target:complete:
内部功能将块保存到您的媒体资源中:
self.myNewBlockProperty = complete;
并在doneCropping:rect
内调用该属性。
您无法访问'完成'其他函数中的参数但你可以将它保存在另一个变量/属性中,你可以毫无问题地访问它。
答案 1 :(得分:1)
您可以在以后的实例变量中保存稍后要调用的块。
@implementation WhateverClass
{
cropComplete cropCompleteBlock;
}
- (void)cropImage:(UIImage *)image type:(NSInteger)type target:(id)target complete:(cropComplete)complete {
cropCompletionBlock = complete;
CGFloat ratio;
switch (type) {
case 1:
//16:9
ratio = 16/9.0;
break;
case 2:
//4:3
ratio = 4/3.0;
break;
case 3:
//1:1
ratio = 1;
break;
default:
break;
}
ImageCropViewController *vc = [ImageCropViewController new];
vc.delegate = self;
vc.imageToCrop = image;
vc.ratio = ratio;
UIViewController *targetVC = (UIViewController *)target;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[targetVC presentViewController:nav animated:YES completion:nil];
}
//this is the delegate from ImageCropViewController above
- (void)doneCropping:(UIImage *)croppedImage rect:(CGRect)rect {
cropCompletionBlock(croppedImage);
cropCompletionBlock = nil;
}
@end