问题
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();
}
回答1:
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.
回答2:
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
}
回答3:
I tried updating the answer but it was nonsensically denied. Here's the latest syntax:
Swift 3.1
let blurEffect = UIBlurEffect(style: .dark)
let blurView = UIVisualEffectView(effect: blurEffect)
self.view.addSubview(blurView)
来源:https://stackoverflow.com/questions/26527411/blur-uiimage-to-achieve-effect-like-passcode-blur-of-the-wallpaper-with-swift