使用另一个函数的完成块
use completion block from another function
我有一个带有委托方法的图像裁剪功能,returns 裁剪图像和 CGRect。我如何 return 在另一个函数中的自定义完成块中执行此操作?
有没有办法引用那个块,以便我可以在另一个函数中使用它?
很难解释,但这是我的代码:
- (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)
}
向您的 class.
添加一个新的 属性 您稍后要调用的块类型 (^(cropComplete)
)
在 cropImage:type:target:complete:
函数中将块保存到您的 属性:
self.myNewBlockProperty = complete;
并在 doneCropping:rect
内调用 属性.
您无法在其他函数中访问 'complete' 参数,但您可以将其保存在另一个 variable/property 中,并且您可以毫无问题地访问它。
您可以简单地将稍后要调用的块保存在实例变量中。
@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
我有一个带有委托方法的图像裁剪功能,returns 裁剪图像和 CGRect。我如何 return 在另一个函数中的自定义完成块中执行此操作?
有没有办法引用那个块,以便我可以在另一个函数中使用它?
很难解释,但这是我的代码:
- (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)
}
向您的 class.
添加一个新的 属性 您稍后要调用的块类型 (^(cropComplete)
)
在 cropImage:type:target:complete:
函数中将块保存到您的 属性:
self.myNewBlockProperty = complete;
并在 doneCropping:rect
内调用 属性.
您无法在其他函数中访问 'complete' 参数,但您可以将其保存在另一个 variable/property 中,并且您可以毫无问题地访问它。
您可以简单地将稍后要调用的块保存在实例变量中。
@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