I wanted to create a blur effect on an UIImageView with the code below. The problem is that I can see the BlurEffect when I run it in the Simulator but not when I connect my iPhone, here I can only see a gray background....any ideas?
Here is the code I used:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.myImageView.image = UIImage(named: "Desert.png")
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame.size = CGSize(width: 375, height: 667)
blurView.center = myImageView.center
self.myImageView.addSubview(blurView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am running on Xcode 6.1.1 (also tried Xcode 6.2!) and my iPhone OS is 8.1.2
Check if the user hasn't disabled transparency effects:
if !UIAccessibilityIsReduceTransparencyEnabled() {
//your code for blur
} else {
//do something else, add a solid color, etc
}
Related
I have used the code as below. But it only blur one position. Now I want to blur multiple positions at once
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func btn_blur(_ sender: Any) {
// 1
let darkBlur = UIBlurEffect(style: .dark)
// 2
let blurView = UIVisualEffectView(effect: darkBlur)
blurView.frame = CGRect(x: 20, y: 30, width: 50, height: 50)
// 3
imageView.addSubview(blurView)
}
}
You could store an array of frames in your view controller, and fill that with your server response. Then use those frames to create the blur views.
import UIKit
class ViewController: UIViewController {
var frames = [CGRect]() // load this from your server data
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func btn_blur(_ sender: Any) {
let darkBlur = UIBlurEffect(style: .dark)
for frame in frames {
let blurView = UIVisualEffectView(effect: darkBlur)
blurView.frame = frame
imageView.addSubview(blurView)
}
}
}
Picture of my code
import UIKit
class ViewController: UIViewController {
#IBOutlet var addItemView: UIView!
#IBOutlet weak var visualEffectView: UIVisualEffectView!
var effect: UIVisualEffect!
override func viewDidLoad() {
super.viewDidLoad()
effect = visualEffectView.effect
visualEffectView.effect = nil
addItemView.layer.cornerRadius = 5
}
func animateIn(){
self.view.addSubview(addItemView)
addItemView.center = self.view.center
addItemView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
addItemView.alpha = 0
UIView.animate(withDuration: 0.4){
self.visualEffectView.effect = self.effect
self.addItemView.alpha = 1
self.addItemView.transform = CGAffineTransform.identity
}
}
#IBAction func sourcesButton(_ sender: Any) {
animateIn()
}
#IBAction func sourceDone(_ sender: Any) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am trying to make a blurry modal that pops up when the user clicks a button, but for some reason this code is not executing. I was following a tutorial on youtube (Youtube Link) I would appreciate the help.
Create blur view like this instead:
var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
var blurEffectView = UIVisualEffectView(effect: blurEffect)
view.addSubview(blurEffectView)
And after it is created you can set alpha to 0 and animate it to 1.
Please note that animation with UIBlurEffect looks horrible on the device :p
I'm attempting to initially blur a background image on a UIViewController. When an #IBAction is called, I'd like to unblur backgroundImage. I can get the image to initially blur, but I can't get rid of the blur if I initially blur it. However, if I don't initially blur backgroundImage, my method works as desired by toggling between a blurred and unblurred image.
These are the relevant variables on my UIViewController class.
#IBOutlet weak var backgroundImage: UIImageView!
// I also leave this hanging around so I can add/remove it as needed
var blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
When the view loads, I'd initially like backgroundImage to have a UIVisualEffectView over it. I initially tried to call toggleBlurView in viewDidLoad, but the display of the UIBlurEffect was off, so I added the code for toggleBlurView in viewDidLayoutSubviews, which fixed that problem.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// if I comment this out, the toggleBlurView() method works as desired
toggleBlurView()
}
Here's my toggleBlurView method.
func toggleBlurView() {
// if there's a blurView, remove it...
if blurView.isDescendant(of: backgroundImage) {
blurView.removeFromSuperview()
}
// otherwise, add blurView to backgroundImage
else {
blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
blurView.frame = backgroundImage.bounds
backgroundImage.addSubview(blurView)
}
}
I have an #IBAction that, when called, also runs the toggleBlurView method.
#IBAction func playFrequency(_ sender: UIButton) {
// do stuff
toggleBlurView()
}
My code does not crash, but I can't get the desired results. I'm not sure if I've found a bug or I'm doing something wrong. I'm wagering the latter. Thank you for reading and I welcome suggestions how to set an image to an initial state of blurred.
I got the following code working as you want it to. I changed up the toggleBlurView function so it's a little more flexible, but this shouldn't affect the functionality.
//
// ViewController.swift
// tableviewheader-test
//
// Created by Forest Kunecke on 3/29/17.
// Use this code for freeeeee!!!! Free as in free beer!!
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myImage: UIImageView!
var blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
override func viewDidLoad() {
super.viewDidLoad()
toggleBlurView(myImage)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func toggleBlur(_ sender: Any) {
toggleBlurView(myImage)
}
func toggleBlurView(_ imageView: UIImageView) {
// if there's a blurView, remove it...
if blurView.isDescendant(of: imageView) {
blurView.removeFromSuperview()
}
// otherwise, add blurView to backgroundImage
else {
blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
blurView.frame = imageView.bounds
imageView.addSubview(blurView)
}
}
}
Here is a screenshot of my storyboard:
I am a beginner so please bear with me. I am trying to load a gif onto one of my pages, and so far I have figured out how to do it on the initial screen, but I would like the gif to load on another page instead. I know I should move it out of the 'override func viewDidLoad() {}' function, but I don't know what to do afterwards. Could somebody please help me? Thank you!
Here is the code so far:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("fire", withExtension: "gif")!)
let imageGif = UIImage.gifWithData(imageData!)
let imageView = UIImageView(image: imageGif)
imageView.frame = CGRect(x: 0.0, y:0.0, width:414.0, height: 673.0)
view.addSubview(imageView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
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)
}
}
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()
}
but NSCoder required... What is it? maybe my way is a wrong way?
You don't need it. The reason why you're not seeing results is because you're attempting to initialize the class from a different initializer than the one you setup.
Try:
class BlurImageView: UIImageView {
override func awakeFromNib() {
super.awakeFromNib()
}
override init() {
var blur:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
var effectView:UIVisualEffectView = UIVisualEffectView (effect: blur)
effectView.frame = frame
addSubview(effectView)
}
}
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.