我想模糊我的 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 ?
最佳答案
问题不是它没有模糊所有图像,而是模糊扩展了图像的边界,使图像变大,结果没有正确对齐。
为了保持图像大小相同,在行之后:
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/