xcode 快速模糊UIImage,实现壁纸密码模糊等效果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26527411/
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 UIImage to achieve effect like passcode blur of the wallpaper with swift
提问by Ben
I want to achieve the effect that passcode view have (the wallpaper picture is blurred and darker)
我要实现密码查看的效果(壁纸图片模糊变暗)
This give me a black screen
这给了我一个黑屏
func blurWithCoreImage(){
var inputImage = CIImage(image: UIImage(named: "wallpaper"));
// Apply gaussian blur filter with radius of 30
var gaussianBlurFilter = CIFilter(name: "CIGaussianBlur");
gaussianBlurFilter.setValue(inputImage, forKey: "inputImage")
gaussianBlurFilter.setValue(30, forKey: "inputRadius")
var context = CIContext(options:nil)
var cgImage = context.createCGImage(gaussianBlurFilter.outputImage, fromRect: inputImage.extent())
// Set up output context.
UIGraphicsBeginImageContext(self.view.frame.size);
var outputContext = UIGraphicsGetCurrentContext();
// Invert image coordinates
CGContextScaleCTM(outputContext, 1.0, -1.0);
CGContextTranslateCTM(outputContext, 0, -self.view.frame.size.height);
// Draw base image.
CGContextDrawImage(outputContext, self.view.frame, cgImage);
// Apply white tint
CGContextSaveGState(outputContext);
// CGContextSetFillColorWithColor(outputContext, UIColor.blackColor().CGColor!);
CGContextFillRect(outputContext, self.view.frame);
CGContextRestoreGState(outputContext);
// Output image is ready.
var outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
backgroundImg.image = outputImage;
}
This function make the image to blur but allso shrink the image and is too far from effect in passcode view
此功能使图像模糊但也会缩小图像并且在密码视图中效果太远
func blurWithCoreImage2(){
var ciimage = CIImage(image: UIImage(named: "passcode"))
var filter = CIFilter(name:"CIGaussianBlur")
filter.setDefaults()
filter.setValue(ciimage, forKey: kCIInputImageKey)
filter.setValue(1, forKey: kCIInputRadiusKey)
var outputImage = filter.outputImage;
var finalImage = UIImage(CIImage: outputImage);
backgroundImg.image = finalImage;
UIGraphicsEndImageContext();
}
回答by JoeFryer
As you only need to support iOS 8 and above you can use the new UIVisualEffectView
, which is the new built-in way of adding blur.
由于您只需要支持 iOS 8 及更高版本,您可以使用 new UIVisualEffectView
,这是添加模糊的新内置方式。
It's very easy to use, something like the following:
它非常易于使用,类似于以下内容:
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = myFrame
self.view.addSubview(blurView)
You can see that you can specifiy a UIBlurEffectStyle
, which will allow you to make it darker as well.
您可以看到您可以指定 a UIBlurEffectStyle
,这也将允许您使其变暗。
回答by Jay
Try this to blur and crop image
试试这个来模糊和裁剪图像
func applyBlurEffect(image: UIImage){
var imageToBlur = CIImage(image: image)
var blurfilter = CIFilter(name: "CIGaussianBlur")
blurfilter.setValue(5, forKey: kCIInputRadiusKey)
blurfilter.setValue(imageToBlur, forKey: "inputImage")
var resultImage = blurfilter.valueForKey("outputImage") as! CIImage
var blurredImage = UIImage(CIImage: resultImage)
var cropped:CIImage=resultImage.imageByCroppingToRect(CGRectMake(0, 0,imageToBlur.extent().size.width, imageToBlur.extent().size.height))
blurredImage = UIImage(CIImage: cropped)
self.backgroundImage.image = blurredImage
}
回答by John R Perry
I tried updating the answer but it was nonsensically denied. Here's the latest syntax:
我尝试更新答案,但被荒谬地拒绝了。这是最新的语法:
Swift 3.1
斯威夫特 3.1
let blurEffect = UIBlurEffect(style: .dark)
let blurView = UIVisualEffectView(effect: blurEffect)
self.view.addSubview(blurView)