Search code examples
iphoneiosxcodegpuimage

GPUImage change brightness with UISlider


How can i change the brightness of an Image with slider using GPUImageBrightnessFilter ?

I tried,

-(void)updateBrightness {
GPUImageFilter *selectedFilter = nil;
[selectedFilter removeAllTargets];
selectedFilter = [[GPUImageFilter alloc] init];

CGFloat midpoint = [(UISlider *)sender value];
[(GPUImageBrightnessFilter *)settingsFilter setBrightness:midpoint];        

UIImage *filteredImage = [selectedFilter imageByFilteringImage:_image_view.image];
fx_imageView.image = filteredImage;

}

Solution

  • There are several problems with the above code.

    First, you're not actually using a brightness filter against your image, because you're calling -imageByFilteringImage: on selectedFilter, which is a generic GPUImageFilter that you allocated fresh. Your GPUImageBrightnessFilter of settingsFilter is never used.

    Second, you don't want to be allocating a new filter with every time you update a parameter. Allocate your GPUImageBrightnessFilter once and simply update it as values change.

    Third, you don't want to keep re-filtering UIImages. Going to and from UIImages is a slow process (and won't work properly when using -imageByFilteringImage: on the same filter, because of some caching I do). Instead, create a GPUImagePicture based on your original image, add a GPUImageBrightnessFilter to that as a target, and target your GPUImageBrightnessFilter at a GPUImageView. Use -processImage every time you update your brightness filter and your updates will be much, much faster. When you need to extract your final image, use -imageFromCurrentlyProcessedOutput.