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();
}
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.
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.