IOS 图片滤镜效果处理
图片色彩滤镜处理
/**
* 图片色彩滤镜处理
*
* @param _inputImage 原始需要处理的图片
* @param _saturation 饱和度(0.f - 2.f)
* @param _brightness 亮 度(-1 - 1)
* @param _contrast 对比度(0.f - 2.f)
*
* @return UIImage
*/
+(UIImage *)imageColorControlsFilterSet:(UIImage *)_inputImage andSaturation:(float)_saturation andBrightness:(float)_brightness withContrast:(float)_contrast{
UIImage *outputImage = _inputImage;
// [S] 滤镜处理
@autoreleasepool {
//创建图像上下文 CIContext
CIContext *_context = [CIContext contextWithOptions:nil];
//创建滤镜CIFiter
CIFilter *_colorControlsFilter = [CIFilter filterWithName:@"CIColorControls"];
//创建过滤源图片CIImage
CIImage *_image = [CIImage imageWithCGImage:_inputImage.CGImage];
[_colorControlsFilter setValue:_image forKey:kCIInputImageKey];
// [S] 设置滤镜参数【可选】
//调整饱和度(0.f - 2.f)
[_colorControlsFilter setValue:[NSNumber numberWithFloat:_saturation] forKey:@"inputSaturation"];
//调整亮度(-1.f - 1.f)
[_colorControlsFilter setValue:[NSNumber numberWithFloat:_brightness] forKey:@"inputBrightness"];
//对比度(0.f - 2.f)
[_colorControlsFilter setValue:[NSNumber numberWithFloat:_contrast] forKey:@"inputContrast"];
// [E] 设置滤镜参数【可选】
//取得输出图片显示或保存
_image = [_colorControlsFilter outputImage];
CGImageRef temp = [_context createCGImage:_image fromRect:[_image extent]];
outputImage = [UIImage imageWithCGImage:temp];
//释放CGImage对象
CGImageRelease(temp);
}
// [E] 滤镜处理
return outputImage;
}
图片高斯模糊滤镜处理
/**
* 图片高斯模糊滤镜处理
*
* @param _inputImage 原始需要处理的图片
* @param _radius 迷糊值(默认为 10)
*
* @return UIImage
*/
+(UIImage *)imageGaussianBlurFilterWithImage:(UIImage *)_inputImage andRadius:(float)_radius{
UIImage *outputImage = _inputImage;
@autoreleasepool {
//创建上下文
CIContext *context = [CIContext contextWithOptions:nil];
//创建滤镜(高斯模糊)
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
//创建过滤源图片
CIImage *image = [[CIImage alloc] initWithCGImage:_inputImage.CGImage];
[filter setValue:image forKey:kCIInputImageKey];
//设置过滤参数
[filter setValue:[NSNumber numberWithFloat:_radius] forKey:@"inputRadius"];
//取得输出图片显示或保存
image = [filter valueForKey:kCIOutputImageKey];
CGImageRef temp = [context createCGImage:image fromRect:[image extent]];
outputImage = [UIImage imageWithCGImage:temp];
//释放
CGImageRelease(temp);
}
return outputImage;
}
/**
* 图片高斯模糊滤镜处理
*
* @param _inputImage 原始需要处理的图片
* @param _radius 迷糊值(默认为 10)
*
* @return UIImage
*/
+(UIImage *)imageGaussianBlurFilterWithUrl:(NSURL *)_imgUrl andRadius:(float)_radius{
UIImage *outputImage;
CIImage *image = [CIImage imageWithContentsOfURL:_imgUrl];
//创建上下文
CIContext *context = [CIContext contextWithOptions:nil];
//创建滤镜(高斯模糊)
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
//创建过滤源图片
[filter setValue:image forKey:kCIInputImageKey];
//设置过滤参数
[filter setValue:[NSNumber numberWithFloat:_radius] forKey:@"inputRadius"];
//取得输出图片显示或保存
image = [filter valueForKey:kCIOutputImageKey];
CGImageRef temp = [context createCGImage:image fromRect:[image extent]];
outputImage = [UIImage imageWithCGImage:temp];
//释放
CGImageRelease(temp);
return outputImage;
}
以上方法只是抛砖引玉,更多详细介绍请自行查找资料,谢谢