前言
这两天在研究如何利用UISlider来调节图片的亮度,这里给出两个版本,一种是将亮度设定死,第二种是可以利用UISlider来调节亮度,仅供初学者们一起探讨交流。
一 将亮度设定死:
- (void)brightness1{
// 目标图片
UIImage *image = [UIImage imageNamed:@"placeholder"];
// 1.在GPUImageBrightnessFilter中首先初始化该滤镜
GPUImageBrightnessFilter *brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
// 2.设置亮度值为0.5
brightnessFilter.brightness = 0.5;
// 3.设置亮度调整范围为整张图像
[brightnessFilter forceProcessingAtSize:image.size];
[brightnessFilter useNextFrameForImageCapture];
// 4.获取数据源
GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:image];
// 5.添加上滤镜
[pic addTarget:brightnessFilter];
// 6.处理图像,开始渲染
[pic processImage];
NSLog(@"Hello World");
// 7.获取处理后的图像
UIImage *imageBrightness = [brightnessFilter imageFromCurrentFramebuffer];
// 8.加载出来
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = imageBrightness;
imageView.frame = CGRectMake(0, 0, imageBrightness.size.width, imageBrightness.size.height);
[self.view addSubview:imageView];
}
二 利用UISlider来调节亮度:
@interface ViewController ()
//数据源,用强引用
@property (nonatomic, strong) GPUImagePicture *pic;
//亮度滤镜
@property (nonatomic, weak) GPUImageBrightnessFilter *brightnessFilter;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self brightness2];
}
- (void)brightness2{
UIImage *image = [UIImage imageNamed:@"placeholder"];
// 在GPUImageBrightnessFilter中首先初始化该滤镜
GPUImageBrightnessFilter *brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
_brightnessFilter = brightnessFilter;
// 设置亮度调整范围为整张图像
[brightnessFilter forceProcessingAtSize:image.size];
[brightnessFilter useNextFrameForImageCapture];
// 获取数据源
GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:image smoothlyScaleOutput:YES];
_pic = pic;
// 创建最终预览的view
GPUImageView *gpuView = [[GPUImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view insertSubview:gpuView atIndex:0];
// 设置GPUImage响应链,从数据源 => 滤镜 => 最终界面效果
[pic addTarget:brightnessFilter]; // ① 添加上滤镜
[brightnessFilter addTarget:gpuView]; // ② 添加效果界面
// 创建slider
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(25.0, [UIScreen mainScreen].bounds.size.height - 190, [UIScreen mainScreen].bounds.size.width - 50.0, 40.0)];
[slider addTarget:self action:@selector(updateSliderValue:) forControlEvents:UIControlEventValueChanged];
slider.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
slider.minimumValue = 0.0;
slider.maximumValue = 1.0;
slider.value = 0.0;
[gpuView addSubview:slider];
// 设置亮度值。
brightnessFilter.brightness = slider.value;
// 数据源处理图像,开始渲染
[pic processImage];
}
- (void)updateSliderValue:(UISlider *)sender
{
_brightnessFilter.brightness = sender.value;
// 数据源,给我重新处理下图片
[_pic processImage];
}
}
这两个方案的区别在于,想利用slider来动态调节图片亮度,那么这里就必须用到GPUImageView来预览最终的效果了,不能直接用UIImageView。
参考资料:
http://blog.csdn.net/sinat_30162391/article/details/51085651
http://www.cnblogs.com/salam/p/5118704.html
http://www.cnblogs.com/qianyindichang/p/4023717.html
http://www.jianshu.com/p/4646894245ba