xcode 通过objective-c模糊图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28613713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Blur an image via objective-c
提问by Deepak Chaudhary
I want to blur an UIImage for an app, Does any of you have an Objective-C method to blur an image ?
我想为一个应用程序模糊一个 UIImage,你们有没有一个 Objective-C 的方法来模糊一个图像?
I tried to find a method or a technique but couldn't find anything. Help please !
我试图找到一种方法或技术,但找不到任何东西。请帮忙 !
回答by Daniel Ornelas
You can use core image filters. http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html
您可以使用核心图像过滤器。 http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html
Look at this snippet from https://gist.github.com/betzerra/5988604
看看来自https://gist.github.com/betzerra/5988604 的这个片段
// Needs CoreImage.framework
- (UIImage *)blurredImageWithImage:(UIImage *)sourceImage{
// Create our blurred image
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [CIImage imageWithCGImage:sourceImage.CGImage];
// Setting up Gaussian Blur
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:[NSNumber numberWithFloat:15.0f] forKey:@"inputRadius"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
/* CIGaussianBlur has a tendency to shrink the image a little, this ensures it matches
* up exactly to the bounds of our original image */
CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
UIImage *retVal = [UIImage imageWithCGImage:cgImage];
if (cgImage) {
CGImageRelease(cgImage);
}
return retVal;
}
回答by Deepak Chaudhary
You can use UIVisualEffectView with visual effects. Initialize an element of visualEffect and effectView than add to your view or imgview wherever you want :). Also you can choose EffectStyles. code snippet :
您可以将 UIVisualEffectView 与视觉效果一起使用。初始化一个 visualEffect 和 effectView 元素,而不是添加到您的视图或 imgview 任何您想要的地方:)。您也可以选择效果样式。代码片段:
UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
visualEffectView.frame = YourImgView.bounds;
[YourImgView addSubview:visualEffectView];
回答by Deepak Kumar
Solution :
解决方案 :
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
blurEffectView.frame = self.view.bounds;
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//ADD BLUR EFFECT VIEW IN MAIN VIEW
[self.view addSubview:blurEffectView];
回答by Dheeraj D
Swift 2.2 :
斯威夫特 2.2:
func blurredImage(with sourceImage: UIImage) -> UIImage {
let filter = CIFilter(name: "CIGaussianBlur")
filter!.setValue(CIImage(image: sourceImage), forKey: kCIInputImageKey)
filter!.setValue(0.8, forKey: kCIInputIntensityKey)
let ctx = CIContext(options:nil)
let cgImage = ctx.createCGImage(filter!.outputImage!, fromRect:filter!.outputImage!.extent)
return UIImage(CGImage:cgImage)
}
回答by Patrik Vaberer
I recommend to use Storyboard for implementing blur or vibrancy by UIVisualEffectView
. For more, look at my sample project at https://github.com/Vaberer/BlurTransitionto demonstrate how to it works and how use it with autolayout inside the UIVisualEffect
我建议使用 Storyboard 通过UIVisualEffectView
. 有关更多信息,请查看https://github.com/Vaberer/BlurTransition上的示例项目,以演示它的工作原理以及如何在 UIVisualEffect 中将其与自动布局一起使用