最近一直在看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); 这就是回调的函数,在这个函数里面把需要的参数处理完后就传到这个函数里面,然后在实现的时候就可以获取这个参数了。