刚刚弄明白,原来block是回调的

  最近一直在看block的使用,一直不明白这种格式

[stillCamera capturePhotoAsImageProcessedUpToFilter:cropFilter

                                      withCompletionHandler:^(UIImage *processed, NSError *error) {

                                          isStatic = YES;

                                          runOnMainQueueWithoutDeadlocking(^{

                                              @autoreleasepool {

                                                  [stillCamera stopCameraCapture];

                                                  [self removeAllTargets];

                                                  [self.retakeButton setHidden:NO];

                                                  [self.cameraToggleButton setEnabled:NO];

                                                  [self.flashToggleButton setEnabled:NO];

                                                  staticPicture = [[GPUImagePicture alloc] initWithImage:processed smoothlyScaleOutput:YES];

                                                  [self prepareFilter];

                                                  [self.photoCaptureButton setTitle:@"Done" forState:UIControlStateNormal];

                                                  [self.photoCaptureButton setImage:nil forState:UIControlStateNormal];

                                                  [self.photoCaptureButton setEnabled:YES];

                                                  if(![self.filtersToggleButton isSelected]){

                                                      [self showFilters];

                                                  }

                                              }

                                          });

                                      }];

里面的:^( UIImage *processed,  NSError *error) 这两个参数是怎么传过来的,现在明白了,原来是回调的,下面看一下原函数就明白了

- (void)capturePhotoAsImageProcessedUpToFilter:(GPUImageOutput<GPUImageInput> *)finalFilterInChain withCompletionHandler:(void (^)(UIImage *processedImage, NSError *error))block;

{

    dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_FOREVER);


    [photoOutput captureStillImageAsynchronouslyFromConnection:[[photoOutput connections] objectAtIndex:0] completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {


        // For now, resize photos to fix within the max texture size of the GPU

        CVImageBufferRef cameraFrame = CMSampleBufferGetImageBuffer(imageSampleBuffer);

        

        CGSize sizeOfPhoto = CGSizeMake(CVPixelBufferGetWidth(cameraFrame), CVPixelBufferGetHeight(cameraFrame));

        CGSize scaledImageSizeToFitOnGPU = [GPUImageOpenGLESContext sizeThatFitsWithinATextureForSize:sizeOfPhoto];

        if (!CGSizeEqualToSize(sizeOfPhoto, scaledImageSizeToFitOnGPU))

        {

            CMSampleBufferRef sampleBuffer;

            GPUImageCreateResizedSampleBuffer(cameraFrame, scaledImageSizeToFitOnGPU, &sampleBuffer);


            dispatch_semaphore_signal(frameRenderingSemaphore);

           [self captureOutput:photoOutput didOutputSampleBuffer:sampleBuffer fromConnection:[[photoOutput connections] objectAtIndex:0]];

            dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_FOREVER);

            CFRelease(sampleBuffer);

        }

        else

        {

            dispatch_semaphore_signal(frameRenderingSemaphore);

            [self captureOutput:photoOutput didOutputSampleBuffer:imageSampleBuffer fromConnection:[[photoOutput connections] objectAtIndex:0]];

            dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_FOREVER);

        }


        UIImage *filteredPhoto = [finalFilterInChain imageFromCurrentlyProcessedOutput];

        dispatch_semaphore_signal(frameRenderingSemaphore);

        

        block(filteredPhoto, error);        

    }];

    

    return;

}

注意最后的那个地方 block(filteredPhoto, error);  这就是回调的函数,在这个函数里面把需要的参数处理完后就传到这个函数里面,然后在实现的时候就可以获取这个参数了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孙启超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值