gpt4 book ai didi

xcode - 模糊 UIImage 以快速实现类似于墙纸密码模糊的效果

转载 作者:搜寻专家 更新时间:2023-11-01 05:45:44 25 4
gpt4 key购买 nike

我想实现passcode view的效果(壁纸图片模糊变暗)

黑屏

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;
}

此功能使图像模糊但也会缩小图像并且在密码 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();

}

最佳答案

由于您只需要支持 iOS 8 及更高版本,您可以使用新的 UIVisualEffectView,这是添加模糊的新内置方式。

使用起来非常简单,类似下面的东西:

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = myFrame
self.view.addSubview(blurView)

您可以看到您可以指定一个 UIBlurEffectStyle,这样您也可以将其调暗。

关于xcode - 模糊 UIImage 以快速实现类似于墙纸密码模糊的效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26527411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com