Reputation: 3134
I have an image cropping function with a delegate method that returns the cropped image and the CGRect. how can I return this in my custom completion block that is in another function?
Is there a way to make a reference to that block so I can use it in another function?
hard to explain, but here is my code:
- (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)
}
Upvotes: 0
Views: 96
Reputation: 20274
You can simply save the block to be called later in an instance variable.
@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
Upvotes: 1
Reputation: 25459
Add a new property of type of the block you want to call later (^(cropComplete)
) to your class.
Inside cropImage:type:target:complete:
function save the block to your property:
self.myNewBlockProperty = complete;
and inside doneCropping:rect
call the property.
You cannot access the 'complete' argument inside other function but you can save it in another variable/property and you can access it with no problem.
Upvotes: 2