ios – 如何制作精确的模糊效果

ios – 如何制作精确的模糊效果,第1张

概述我想Blur实际上像第一个Image. 我做了一些代码并使它像第二个图像. 我的模糊代码就像 let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.layer.opacity


我想Blur实际上像第一个Image.

我做了一些代码并使它像第二个图像.

我的模糊代码就像

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)    blurEffectVIEw = UIVisualEffectVIEw(effect: blurEffect)    blurEffectVIEw.layer.opacity = 0.8    blurEffectVIEw.Alpha = 0.6    blurEffectVIEw.frame = CGRectMake(0,42,UIScreen.mainScreen().bounds.wIDth,UIScreen.mainScreen().bounds.height - 42)    sourceVIEw.addSubvIEw(blurEffectVIEw)

SourcevIEw是我的背景视图.我想让它变得模糊.有什么建议吗?

解决方法 Alpha和layer.opacity更正不是必需的,您也可以使用扩展名进行更正:

extension UIImageVIEw{       func makeBlurImage(imageVIEw:UIImageVIEw?)       {          let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)          let blurEffectVIEw = UIVisualEffectVIEw(effect: blurEffect)          blurEffectVIEw.frame = imageVIEw!.bounds          blurEffectVIEw.autoresizingMask = [.FlexibleWIDth,.FlexibleHeight] // to support device rotation          imageVIEw?.addSubvIEw(blurEffectVIEw)       }    }

用法:

let imageVIEw = UIImageVIEw(frame: CGRectMake(0,100,300,400))  let image:UIImage = UIImage(named: "photo.png")!  imageVIEw.image =  image  //Apply blur effect  imageVIEw.makeBlurImage(imageVIEw)  self.vIEw.addSubvIEw(imageVIEw)

但是如果你想将模糊效果应用到UIVIEw,你可以使用这个代码:

protocol Blurable{    var layer: CALayer { get }    var subvIEws: [UIVIEw] { get }    var frame: CGRect { get }    var supervIEw: UIVIEw? { get }    func addSubvIEw(vIEw: UIVIEw)    func removeFromSupervIEw()    func blur(blurRadius blurRadius: CGfloat)    func unBlur()    var isBlurred: Bool { get }}extension Blurable{    func blur(blurRadius blurRadius: CGfloat)    {        if self.supervIEw == nil        {            return        }        UIGraphicsBeginImageContextWithOptions(CGSize(wIDth: frame.wIDth,height: frame.height),false,1)        layer.renderInContext(UIGraphicsGetCurrentContext()!)        let image = UIGraphicsGetimageFromCurrentimageContext()        UIGraphicsEndImageContext();        guard let blur = CIFilter(name: "CIGaussianBlur"),this = self as? UIVIEw else        {            return        }        blur.setValue(CIImage(image: image),forKey: kCIInputimageKey)        blur.setValue(blurRadius,forKey: kCIinputRadiusKey)        let ciContext  = CIContext(options: nil)        let result = blur.valueForKey(kCIoUtputimageKey) as! CIImage!        let boundingRect = CGRect(x:0,y: 0,wIDth: frame.wIDth,height: frame.height)        let cgImage = ciContext.createCGImage(result,fromrect: boundingRect)        let filteredImage = UIImage(CGImage: cgImage)        let blurOverlay = BlurOverlay()        blurOverlay.frame = boundingRect        blurOverlay.image = filteredImage        blurOverlay.contentMode = UIVIEwContentMode.left        if let supervIEw = supervIEw as? UIStackVIEw,index = (supervIEw as UIStackVIEw).arrangedSubvIEws.indexOf(this)        {            removeFromSupervIEw()            supervIEw.insertArrangedSubvIEw(blurOverlay,atIndex: index)        }        else        {            blurOverlay.frame.origin = frame.origin            UIVIEw.TransitionFromVIEw(this,toVIEw: blurOverlay,duration: 0.2,options: UIVIEwAnimationoptions.CurveEaseIn,completion: nil)        }        objc_setAssociatedobject(this,&BlurableKey.blurable,blurOverlay,objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)    }    func unBlur()    {        guard let this = self as? UIVIEw,blurOverlay = objc_getAssociatedobject(self as? UIVIEw,&BlurableKey.blurable) as? BlurOverlay else        {            return        }        if let supervIEw = blurOverlay.supervIEw as? UIStackVIEw,index = (blurOverlay.supervIEw as! UIStackVIEw).arrangedSubvIEws.indexOf(blurOverlay)        {            blurOverlay.removeFromSupervIEw()            supervIEw.insertArrangedSubvIEw(this,atIndex: index)        }        else        {            this.frame.origin = blurOverlay.frame.origin            UIVIEw.TransitionFromVIEw(blurOverlay,toVIEw: this,nil,objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)    }    var isBlurred: Bool    {        return objc_getAssociatedobject(self as? UIVIEw,&BlurableKey.blurable) is BlurOverlay    }}extension UIVIEw: Blurable{}class BlurOverlay: UIImageVIEw{}struct BlurableKey{    static var blurable = "blurable"}

Swift 4.x

extension UIVIEw {struct BlurableKey {    static var blurable = "blurable"}func blur(radius: CGfloat) {    guard let supervIEw = supervIEw else { return }    UIGraphicsBeginImageContextWithOptions(CGSize(wIDth: frame.wIDth,1)    layer.render(in: UIGraphicsGetCurrentContext()!)    guard let image = UIGraphicsGetimageFromCurrentimageContext() else { return }    UIGraphicsEndImageContext()    guard let blur = CIFilter(name: "CIGaussianBlur") else { return }    blur.setValue(CIImage(image: image),forKey: kCIInputimageKey)    blur.setValue(radius,forKey: kCIinputRadiusKey)    let ciContext  = CIContext(options: nil)    guard let result = blur.value(forKey: kCIoUtputimageKey) as? CIImage else { return }    let boundingRect = CGRect(x: 0,height: frame.height)    guard let cgImage = ciContext.createCGImage(result,from: boundingRect) else { return }    let filteredImage = UIImage(cgImage: cgImage)    let blurOverlay = UIImageVIEw()    blurOverlay.frame = boundingRect    blurOverlay.image = filteredImage    blurOverlay.contentMode = UIVIEwContentMode.left    if let stackVIEw = supervIEw as? UIStackVIEw,let index = stackVIEw.arrangedSubvIEws.index(of: self) {        removeFromSupervIEw()        stackVIEw.insertArrangedSubvIEw(blurOverlay,at: index)    } else {        blurOverlay.frame.origin = frame.origin        UIVIEw.Transition(from: self,to: blurOverlay,options: UIVIEwAnimationoptions.curveEaseIn,completion: nil)    }    objc_setAssociatedobject(self,objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)}func unBlur() {    guard let blurOverlay = objc_getAssociatedobject(self,&BlurableKey.blurable) as? UIImageVIEw else { return }    if let stackVIEw = blurOverlay.supervIEw as? UIStackVIEw,let index = stackVIEw.arrangedSubvIEws.index(of: blurOverlay) {        blurOverlay.removeFromSupervIEw()        stackVIEw.insertArrangedSubvIEw(self,at: index)    } else {        frame.origin = blurOverlay.frame.origin        UIVIEw.Transition(from: blurOverlay,to: self,objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)}var isBlurred: Bool {    return objc_getAssociatedobject(self,&BlurableKey.blurable) is UIImageVIEw}}

用法是例如:

segmentedControl.unBlur()segmentedControl.blur(blurRadius: 2)

这是Blurable项目的来源.

您可以在他的GitHub项目here中找到更多细节

总结

以上是内存溢出为你收集整理的ios – 如何制作精确的模糊效果全部内容,希望文章能够帮你解决ios – 如何制作精确的模糊效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1029798.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存