ios - CIGaussianBlur 图像大小

标签 ios objective-c blur core-image

我想模糊我的 View ,我使用这段代码:

//Get a UIImage from the UIView
NSLog(@"blur capture");
UIGraphicsBeginImageContext(BlurContrainerView.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Blur the UIImage
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 5] forKey: @"inputRadius"]; //change number to increase/decrease blur
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];

//create UIImage from filtered image
blurredImage = [[UIImage alloc] initWithCIImage:resultImage];

//Place the UIImage in a UIImageView
UIImageView *newView = [[UIImageView alloc] initWithFrame:self.view.bounds];
newView.image = blurredImage;

NSLog(@"%f,%f",newView.frame.size.width,newView.frame.size.height);
//insert blur UIImageView below transparent view inside the blur image container
[BlurContrainerView insertSubview:newView belowSubview:transparentView];

它模糊了视野,但不是全部。如何模糊所有 View ?

billed: postimg.org/image/9bee2e4zx/

最佳答案

问题不是它没有模糊所有图像,而是模糊扩展了图像的边界,使图像变大,结果没有正确对齐。

为了保持图像大小相同,在行之后:

CIImage *resultImage    = [gaussianBlurFilter valueForKey: @"outputImage"];

您可以在 resultImage 的中心获取原始图像大小的矩形的 CGRect:

// note, adjust rect because blur changed size of image

CGRect rect             = [resultImage extent];
rect.origin.x          += (rect.size.width  - viewImage.size.width ) / 2;
rect.origin.y          += (rect.size.height - viewImage.size.height) / 2;
rect.size               = viewImage.size;

然后使用 CIContext 获取图像的那部分:

CIContext *context      = [CIContext contextWithOptions:nil];
CGImageRef cgimg        = [context createCGImage:resultImage fromRect:rect];
UIImage   *blurredImage = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgimg);

或者,对于 iOS 7,如果您转到 iOS UIImageEffects sample code并下载iOS_UIImageEffects.zip,然后就可以抓取UIImage+ImageEffects类了。无论如何,这提供了一些新方法:

- (UIImage *)applyLightEffect;
- (UIImage *)applyExtraLightEffect;
- (UIImage *)applyDarkEffect;
- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;
- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;

因此,要对其进行模糊、成像和变亮(产生“毛玻璃”效果),您可以执行以下操作:

UIImage *newImage = [image applyLightEffect];

有趣的是,Apple 的代码没有使用CIFilter,而是调用了vImageBoxConvolve_ARGB8888。的 vImage high-performance image processing framework . WWDC 2013 视频中说明了此技术 Implementing Engaging UI on iOS .

关于ios - CIGaussianBlur 图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20531938/

相关文章:

ios - 如何从另一个 UIViewController 显示 GameCenter 的排行榜

ios - XCode5 中两个 UITableView 之间的奇怪纠缠

javascript - jQuery .blur() - 不要隐藏事件的 div

jQuery 应该模糊全屏背景图像

ios - 在方法中返回后,核心数据实体缺少其属性

ios - 正确设置 numberOfRowsInSection

iphone - 我如何将 Facebook 好友列表存储到 iOS 数组中?

objective-c - 如何查看AFNetworking的Swift API?

java - 如何实现这样的运动模糊效果?

iphone - NSMutableArray : Memory management while calling methods