使用 Swift 对 ImageView 的 iOS 模糊效果 [英] iOS blur effect to ImageView with Swift

查看:23
本文介绍了使用 Swift 对 ImageView 的 iOS 模糊效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作简单的 iOS 应用程序.我需要制作具有模糊效果的图像视图.我在 Stack Overflow 上找到了这段代码:

I am trying to make simple iOS app. I need to make image view with blur effect. I found this code on Stack Overflow:

class BlurImageView: UIImageView {

  override func awakeFromNib() {
    super.awakeFromNib()
  }

  required init(coder aDecoder: NSCoder!){
      super.init(coder: aDecoder)   
      var blur:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
      var effectView:UIVisualEffectView = UIVisualEffectView (effect: blur)
      effectView.frame = frame
      addSubview(effectView)
  }
}

我需要将这个类连接到我的 image_view,但我不知道该怎么做.我尝试在 ViewControllerview_did_load 函数中执行此操作(image_view 是一个出口)

And I need to connect this class to my image_view, but I don't know how to do it. I try to do it in my view_did_load function in my ViewController (image_view is a outlet)

override func viewDidLoad() {
    super.viewDidLoad()
    image_view = BlurImageView()
}

但是 NSCoder 是必需的……它是什么?也许我的方式是错误的方式?

but NSCoder required... What is it? maybe my way is a wrong way?

推荐答案

你需要让你的 image_view 成为 BlurImageView 的一个实例,所以在你的视图控制器和你的image_view 是一个 outlet,你需要让它成为 BlurImageView 的实例,而不是 UIImageView 的实例.

You need to make your image_view an instance of BlurImageView, so in your view controller and where your image_view is an outlet, you need to make it an instance of BlurImageView and not an instance of an UIImageView.

这篇关于使用 Swift 对 ImageView 的 iOS 模糊效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆