简体   繁体   English

通过uislider和gpuimage滤镜更改图像的亮度

[英]Change brightness of an image via uislider and gpuimage filter

I wrote this code to change the brightness of an UIImage via an UISlider and the GPUImageBrightnessFilter. 我编写了这段代码,以通过UISlider和GPUImageBrightnessFilter更改UIImage的亮度。 But every time I'll test it the app crashes. 但是每次我测试时,应用都会崩溃。

My code: 我的代码:

- (IBAction)sliderBrightness:(id)sender {

    CGFloat midpoint = [(UISlider *)sender value];
    [(GPUImageTiltShiftFilter *)brightnessFilter setTopFocusLevel:midpoint - 0.1];
    [(GPUImageTiltShiftFilter *)brightnessFilter setBottomFocusLevel:midpoint + 0.1];

    [sourcePicture processImage];
}

- (void) brightnessFilter {

    UIImage *inputImage = imgView.image;

    sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];
    brightnessFilter = [[GPUImageTiltShiftFilter alloc] init];
    //    sepiaFilter = [[GPUImageSobelEdgeDetectionFilter alloc] init];

    GPUImageView *imageView = (GPUImageView *)self.view;
    [brightnessFilter forceProcessingAtSize:imageView.sizeInPixels]; // This is now needed to make the filter run at the smaller output size

    [sourcePicture addTarget:brightnessFilter];
    [brightnessFilter addTarget:imageView];

    [sourcePicture processImage];
}

Let me make an alternative architectural suggestion. 让我提出另一种体系结构建议。 Instead of creating a GPUImagePicture and GPUImageBrightnessFilter each time you change the brightness, then saving that out as a UIImage to a UIImageView, it would be far more efficient to reuse the initial picture and filter and render that to a GPUImageView. 与其每次更改亮度时都创建GPUImagePicture和GPUImageBrightnessFilter,然后将其另存为UIImage到UIImageView,不如重用初始图片并进行过滤并将其呈现到GPUImageView,效率将大大提高。

Take a look at what I do in the SimpleImageFilter example that comes with GPUImage. 看看我在GPUImage随附的SimpleImageFilter示例中的操作。 For the tilt-shifted image that's displayed to the screen, I create a GPUImagePicture of the source image once, create one instance of the tilt-shift filter, and then send the output to a GPUImageView. 对于显示在屏幕上的倾斜位移图像,我创建了一次源图像的GPUImagePicture,创建了一个倾斜位移滤镜实例,然后将输出发送到GPUImageView。 This avoids the expensive (both performance and memory-wise) process of going to a UIImage and then displaying that in a UIImageView, and will be much, much faster. 这避免了转到UIImage然后在UIImageView中显示它的昂贵(性能和内存方面)的过程,而且速度会快得多。 While you're at it, you can use -forceProcessingAtSize: on your filter to only render as many pixels as will be displayed in your final view, also speeding things up. 使用它时,可以在过滤器上使用-forceProcessingAtSize:来仅渲染最终视图中显示的尽可能多的像素,从而加快处理速度。

When you have the right settings for filtering your image, and you want the final UIImage out, you can do one last render pass to extract the processed UIImage. 如果您具有用于过滤图像的正确设置,并且想要输出最终的UIImage,则可以执行最后一个渲染过程以提取处理后的UIImage。 You'd set your forced size back to 0 right before doing that, so you now process the full image. 您需要在此之前将强制大小设置回0,这样就可以处理整个图像了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM