How to add vibrancy effect to an image - swift

I made a blur image using UIBlureffect and it worked fine.
I would like to add a vibrancy image but so far it has not worked
Current code:
#IBOutlet var imageViewGif: UIImageView!
func addEffect()
{
var effect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
var effectView = UIVisualEffectView(effect: effect)
effectView.frame = CGRectMake(0, 0, 400, 536)
view.addSubview(effectView)
}
I would like to add an image that is not restricted to a preset area,
and I would like the blur effect to adapt to the image area if possible
(the image is not square).
If there is anything more you need, do not hesitate to ask!

I solved this using the hierachy view and added the "visual effect with blur and vibrancy effect" from the object library.

Related

Adding UIImage to new Scroll View

I'm trying to create a simple app in Xcode that lets me press a button and it adds an image to a view. I originally had everything working until I decided to add a scroll view, as I wanted to create multiple buttons. My problem is, the created image follows down when scrolling. Im sure this is an easy fix. My code looks like this for each button:
#IBAction func btnAddColorfulLights1(_ sender: Any) {
let image: UIImage = UIImage(named: "ColorfulLights.png")!
let imageView = UIImageView(image: image)
self.view.addSubview(imageView)
imageView.tag = 10
imageView.frame = CGRect(x: 67, y: 39, width: 240, height: 338)
}
I think my problem is in,
self.view.addSubview(imageView)
Where I'm adding the image to view, where I need to be adding it to the scroll view.
This is my control viewer organization/set up:
Here's what it should look like:
Here's the problem:
I know this is probably very easy but I can't seem to find any answers to my specific problem or anything that can help me. Thank you so much!
You need to add the UIImageView containing the image of your lights to the UIView that is the contentView of the UIScrollView.
An easy way to do this is to create an #IBOutlet for the UIView in your scrollView. control-drag from the View under the ScrollView in the Document Outline to your ViewController code. Give the #IBOutlet a name such as:
#IBOutlet weak var scrollViewsView: UIView!
Then, use it to add your adornments:
#IBAction func btnAddColorfulLights1(_ sender: Any) {
let image = UIImage(named: "ColorfulLights.png")!
let imageView = UIImageView(image: image)
self.scrollViewsView.addSubview(imageView)
imageView.tag = 10
imageView.frame = CGRect(x: 67, y: 39, width: 240, height: 338)
}

How to Make NSVisualEffectView That Only Appears When Content is Beneath It

I am working on a macOS application that makes use of NSVisualEffectView to achieve transparency. I want to achieve an effect similar to that of iOS' Cover Sheet widget view. Notice how when you scroll up, content a blur appears around the search bar. I want do this in a macOS app so that when content in a table view is scrolled beneath a certain point an NSVisualEffectView blurs it. How would I do this? Thanks in advance.
Create a instance of Visual Effect View as following and set on window:
func setVisualEffectToWindow(window: NSWindow) {
// create the visual effect view
var blurryView = NSVisualEffectView(frame: NSRect(x: 0, y: 0, width: 800, height: 600)) //Create with size you want or you can use window content bound here
// this is default value but is here for clarity
blurryView.blendingMode = NSVisualEffectBlendingMode.BehindWindow
// set the background to always be the dark blur
blurryView.material = NSVisualEffectMaterial.Dark
// set it to always be blurry regardless of window state
blurryView.state = NSVisualEffectState.Active
window.contentView.addSubview(blurryView)
}

Laggy UITableView scrolling because of CAShapeLayer (Swift 3)

I have a UITableView within a UIView, and it is made up of custom cells defined by the following class:
class CustomAddFriendTableViewCell: UITableViewCell {
#IBOutlet weak var UsernameLbl: UILabel!
#IBOutlet weak var ProfileImg: UIImageView!
#IBOutlet weak var AddFriendBtn: UIButton!
}
Within the ViewController's tableview(_:cellForRowAt:) method I call the following function to layout the cell's ProfileImg:
private func layoutProfilePics(with cell:CustomAddFriendTableViewCell) {
//create gradient
let gradient = CAGradientLayer()
gradient.frame = CGRect(origin: CGPoint.zero, size: cell.ProfileImg.frame.size)
gradient.colors = [Colors.blueGreen.cgColor, Colors.yellow.cgColor]
//create gradient mask
let shape = CAShapeLayer()
shape.lineWidth = 3
shape.path = UIBezierPath(ovalIn: cell.ProfileImg.bounds).cgPath // commenting this out makes lag go away
shape.strokeColor = UIColor.black.cgColor // commenting this out makes lag go away
shape.fillColor = UIColor.clear.cgColor
gradient.mask = shape
cell.ProfileImg.layoutIfNeeded()
cell.ProfileImg.clipsToBounds = true
cell.ProfileImg.layer.masksToBounds = true
cell.ProfileImg.layer.cornerRadius = cell.ProfileImg.bounds.size.width/2.0
cell.ProfileImg.layer.addSublayer(gradient)
}
This code causes ProfileImg to be a circle and have a border with a blue-green gradient.
The two lines with comments next to them make the scrolling very smooth (meaning the gradient is not what is causing the lag), so I assume that rendering the CAShapeLayer (specifically the stroke) is causing the problem (hence the question title). What can I do to improve the tableview's scrolling performance?
Also, I am not sure if this is an XCode bug or if it has something to do with my problem, but in the Project Navigator's Instruments pane, when I run the app and scroll the laggy UITableView, the FPS does not reflect the lag, even though I can clearly tell that it is very laggy. In fact, there are no noticeable differences in any of the components in the pane (CPU, Memory, Energy Impact, etc.).
Update:
I tried moving the layoutProfilePics(with:) function into the CustomAddFriendTableViewCell's prepareForReuse() function and I also tried putting layoutProfilePics(with:) in its layoutSubviews() function but neither of them improved the scrolling.
That code is meant to be called once for each cell and for that, it shouldn't be called in tableView(_:cellForRowAt:). Try moving that code to awakeFromNib() and if you're using a nib for your custom cell, use the following code in viewDidLoad() to load the the outlets:
let addFriendCellNib = UINib(nibName: "CustomAddFriendTableViewCell", bundle: nil)
tableView.register(addFriendCellNib, forCellReuseIdentifier: "addFriendCell")
I hope that works for you.

Swift - instance of backgroundColor not working

If I set up an image in the Image/Image View of the Attributes Inspector the following subsequent call does not work:
picture.backgroundColor = UIColor.greenColor()
If I setup the image in the code, such as in viewDidAppear, it does.
Am I missing something, is this a quirk and how do I get it to behave?
It does work. But you can't see it in some cases.
For example: in the following image I have change the background color and set it to aspect fit. The gap between the two appears in green. So the image must have some transparency to it in order to see the background color.
Image with white background
Image with transparent background
Edit:
This is how I changed the background color
#IBOutlet weak var picture: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
picture.backgroundColor = UIColor.greenColor()
}

Swift: Round UIButton with Blur background

I am wanting to make a round UIButton but with a light blur effect with vibrancy as it's background
So far I've got a rounded UIButton, but the code I have found online (I'm new to iOS development so don't really understand how the blur etc works) to add a blur just puts it as the entire button's frame, essentially making the button appear square again.
I've also tried adding a view, then the UIButton and then the blur effect and applied the cornerRadius code to that view but it also didn't work.
Here is what I've tried:
shortcutButton.layer.cornerRadius = 0.5 * shortcutButton.bounds.size.width // add the round corners in proportion to the button size
let blur = UIVisualEffectView(effect: UIBlurEffect(style:
UIBlurEffectStyle.Light))
blur.frame = shortcutButton.bounds
blur.userInteractionEnabled = false //This allows touches to forward to the button.
shortcutButton.insertSubview(blur, atIndex: 0)
Add the following two lines of code to your project, before you add the subview:
blur.layer.cornerRadius = 0.5 * shortcutButton.bounds.size.width
blur.clipsToBounds = true
Enjoy! :)