我使用GPUImage框架来获取某些相机输入的平均红色值。我将所有平均红色值存储在一个数组中,我试着这样做:
NSMutableArray *redValues = [NSMutableArray array];
__block int counter = 0;
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)
{
NSLog(@"%f", redComponent);
[redValues addObject:@(redComponent)];
counter++;
}];
for (int i = 0; i < counter; i++)
{
redValues[i] *= 255; // errors occurs here
}
答案 0 :(得分:2)
您得到的错误是因为您试图将NSNumber
乘以int
。但是,存在一个更深层次的逻辑问题,即您将redComponent
作为0到1之间的值接收。当您转换为int
时,几乎将所有值都截断为0(此时乘以255将无济于事)。相反,你应该内联乘法(并消除第二个循环):
averageColor.colorAverageProcessingFinishedBlock = ^(CGFloat redComponent, CGFloat greenComponent, CGFloat blueComponent, CGFloat alphaComponent, CMTime frameTime) {
NSLog(@"%f", redComponent);
[redValues addObject:@(redComponent * 255)];
};