【问题标题】:use completion block from another function使用另一个函数的完成块
【发布时间】:2015-10-09 10:54:23
【问题描述】:

我有一个图像裁剪函数,它带有一个返回裁剪图像和 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)
}

【问题讨论】:

    标签: ios objective-c protocols block


    【解决方案1】:

    将您以后要调用的块类型的新属性 (^(cropComplete)) 添加到您的类中。

    cropImage:type:target:complete: 函数中将块保存到您的属性中:

    self.myNewBlockProperty = complete;
    

    doneCropping:rect 内部调用该属性。

    您无法在其他函数中访问“完整”参数,但可以将其保存在另一个变量/属性中,并且可以毫无问题地访问它。

    【讨论】:

      【解决方案2】:

      您可以简单地将块保存在实例变量中以供稍后调用。

      @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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-30
        • 2018-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-22
        • 2019-11-14
        • 2021-11-01
        相关资源
        最近更新 更多