使用Swift对ImageView的iOS模糊效果 [英] iOS blur effect to ImageView with Swift
问题描述
我正在尝试制作简单的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
,但我不知道该怎么做。
我尝试在 ViewController
中的 view_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
一个<$ c的实例$ c> BlurImageView ,所以在你的视图控制器和你的 image_view
是一个插座的地方,你需要让它成为<$ c $的一个实例c> 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屋!