我正在使用GPUImage框架,我注意到編譯器會自動跳過setColorAverageProcessingFinishedBlock的括號內的所有內容。它完全跳過這些內容並繼續執行代碼中的其他所有內容。一旦執行了其他所有內容,它就會回到括號內的內容。顯然,這具有意想不到的副作用。Objective-C塊不跳過代碼,然後執行它
NSMutableArray *redValues = [NSMutableArray array];
NSMutableArray *arrayOne = [NSMutableArray array];
NSUInteger arrayOneLength = [arrayOne count];
__block int counter = 0;
int amount = 1;
float totalOne, diffForAverage;
NSInteger j;
GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
GPUImageAverageColor *averageColor = [[GPUImageAverageColor alloc] init];
[averageColor setColorAverageProcessingFinishedBlock:^(CGFloat redComponent, CGFloat greenComponent, CGFloat blueComponent, CGFloat alphaComponent, CMTime frameTime)
{ // the compiler runs until here, then skips everything within these brackets
NSLog(@"%f", redComponent);
[redValues addObject:@(redComponent * 255)];
}]; // after the brackets close, it executes everything that is below this
// once everything below this has been executed, it goes back to the brackets and executes
// everything between them
[videoCamera addTarget:averageColor];
[videoCamera startCameraCapture];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 27 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[videoCamera stopCameraCapture];
});
totalOne = [redValues[24] floatValue];
float average = totalOne/amount;
NSUInteger redValuesLength = [redValues count];
for (j = (counter + 24); j < (redValuesLength - 24); j++)
{
diffForAverage = average - [redValues[j + 1] floatValue];
if (diffForAverage > -1 && diffForAverage < 1)
{
totalOne += [redValues[j + 1] floatValue];
amount++;
[arrayOne addObject:[NSNumber numberWithInt:(j - 24)]];
counter++;
}
}
我該如何解決這個問題?
您是否閱讀過'setColorAverageProcessingFinishedBlock'的文檔來理解它應該做什麼? – jtbandes
@jtbandes是的,我已經看到它在使用的例子。沒有像我遇到的事情被列出。 – fi12
對我來說'FinishedBlock'這個詞表明這正是它應該做的。該塊將在「色彩平均處理完成」時執行。 – jtbandes