我使用GPUImage過濾器來創建一個簡單的圖像亮度,對比度和飽和度調整工具。視圖有一個圖像視圖,每個過濾器有三個滑塊。起初我以爲我會應用每個過濾器作爲每個滑塊的價值改變,但後來我意識到,過濾器必須鏈接。所以我寫了下面的代碼。這個代碼的問題是隻有亮度過濾器應用於圖像。應用與UISlider GPUImage過濾器
- (IBAction)brightnessSlider:(UISlider *)sender {
brightnessValue = sender.value;
UIImage *inputImage = [UIImage imageNamed:@"2.jpg"];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
GPUImageSaturationFilter *saturationFilter = [GPUImageSaturationFilter new];
[brightnessFilter setBrightness:brightnessValue];
[contrastFilter setContrast:contrastValue];
[saturationFilter setSaturation:saturationValue];
[stillImageSource addTarget:brightnessFilter];
[brightnessFilter addTarget:contrastFilter];
[contrastFilter addTarget:saturationFilter];
[brightnessFilter useNextFrameForImageCapture];
[stillImageSource processImage];
UIImage *currentFilteredVideoFrame = [brightnessFilter imageFromCurrentFramebuffer];
imageView.image = currentFilteredVideoFrame;
}
- (IBAction)contrastSlider:(UISlider *)sender
{
contrastValue = sender.value;
UIImage *inputImage = [UIImage imageNamed:@"2.jpg"];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
GPUImageSaturationFilter *saturationFilter = [GPUImageSaturationFilter new];
[brightnessFilter setBrightness:brightnessValue];
[contrastFilter setContrast:contrastValue];
[saturationFilter setSaturation:saturationValue];
[stillImageSource addTarget:brightnessFilter];
[brightnessFilter addTarget:contrastFilter];
[contrastFilter addTarget:saturationFilter];
[brightnessFilter useNextFrameForImageCapture];
[stillImageSource processImage];
UIImage *currentFilteredVideoFrame = [brightnessFilter imageFromCurrentFramebuffer];
imageView.image = currentFilteredVideoFrame;
}
- (IBAction)saturationSlider:(UISlider *)sender
{
saturationValue = sender.value;
UIImage *inputImage = [UIImage imageNamed:@"2.jpg"];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
GPUImageSaturationFilter *saturationFilter = [GPUImageSaturationFilter new];
[brightnessFilter setBrightness:brightnessValue];
[contrastFilter setContrast:contrastValue];
[saturationFilter setSaturation:saturationValue];
[stillImageSource addTarget:brightnessFilter];
[brightnessFilter addTarget:contrastFilter];
[contrastFilter addTarget:saturationFilter];
[brightnessFilter useNextFrameForImageCapture];
[stillImageSource processImage];
UIImage *currentFilteredVideoFrame = [brightnessFilter imageFromCurrentFramebuffer];
imageView.image = currentFilteredVideoFrame;
}
現在我決定實施一個更簡單的方法了整個事情,但我仍然得到什麼:
UIImage *inputImage = [UIImage imageNamed:@"2.jpg"];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
//Set Brightness to 60
GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
[brightnessFilter setBrightness:60.0];
//Set Contrast to 12
GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
[contrastFilter setContrast:12];
[brightnessFilter addTarget:contrastFilter];
[stillImageSource addTarget:contrastFilter];
[contrastFilter useNextFrameForImageCapture];
[stillImageSource processImage];
UIImage *outputImage1 = [contrastFilter imageFromCurrentFramebuffer];
imageView.image = outputImage1;
有一兩件事我注意到的是,代碼「[brightnessFilter useNextFrameForImageCapture]。」在調用processImage之前的所有三種方法中都會使用它......這可能是您的問題嗎? – dean