Add blur effect to image Loading from URL - ios

i want to blur image before displaying which is loading from URL.

I have made an extension method for UIImageView to achieve this, you only need to call it once:
import Foundation
import UIKit
public extension UIImageView {
func blurImage() {
let darkBlur = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: darkBlur)
blurView.frame = self.bounds
self.addSubview(blurView)
}
}

Suppose you try to add blur effect to an ImageView , then
Assign downlaoded Image to ImageView
imageView.image = .. code for download image and assign image to imageView.
Then add blur effect
var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
visualEffectView.frame = imageView.bounds
imageView.addSubview(visualEffectView)

Related

Blurred Image as background in UIView

So I have an image that I am rendering in a view. For the background color of that uiview I actually want a UIImage. The way I was doing was taking the currentImageView and applying this function makeBlurEffect.
func makeBlurImage(targetImageView:UIImageView?)
{
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = targetImageView!.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // for supporting device rotation
targetImageView?.addSubview(blurEffectView)
}
I typically set the background image and apply the blur filter inside the lazy var method that creates the UIView as shown below.
lazy var blurryBackGround : UIView = {
let blurryBackGround = UIView()
blurryBackGround.backgroundColor = UIColor.red
// blurryBackGround.backgroundColor = UIColor(i)
var blurryImage = currentImageView
blurryImage.makeBlurImage(blurryImage?)
return blurryBackGround
}()
I have also included the UIImageView creation:
//
lazy var currentEventImage : UIImageView = {
let currentEvent = UIImageView()
currentEvent.clipsToBounds = true
currentEvent.translatesAutoresizingMaskIntoConstraints = false
currentEvent.contentMode = .scaleAspectFill
currentEvent.isUserInteractionEnabled = true
currentEvent.layer.masksToBounds = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handlePromoVid))
currentEvent.isUserInteractionEnabled = true
currentEvent.addGestureRecognizer(tapGestureRecognizer)
return currentEvent
}()
Any idea how i would correctly do this?
The current way I implemented would blur the original image, change the blurred image to the main image and turning the background image to the unblurred image
Use following extension:
extension UIImageView {
func renderedImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
self.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
To capture currently presented imageView to an UIImage.
EDIT
How about you try not capturing it yet, just use this as a background:
lazy var blurryBackGround : UIView = {
let blurryBackGround = currentImageView
blurryBackGround.makeBlurImage(blurryBackGround)
return blurryBackGround
}()

How to make the UIImage edges softer?

I have a UIImage with a UIVisualEffectView on top of it. I want to make the edges softer to give it a shadow effect.
I tried playing around with maskToBounds but it doesn't make a difference.
let weekndImage = UIImage(named: "weeknd.jpg")
let weekndIV = UIImageView(image: weekndImage)
weekndIV.frame.size = CGSize(width: 200, height: 200)
let blur = UIBlurEffect(style: .light)
let blurView = UIVisualEffectView(effect: blur)
blurView.frame = weekndIV.bounds
weekndIV.addSubview(blurView)
weekndIV.layer.cornerRadius = 10
weekndIV.layer.masksToBounds = true
move these two lines after setting the blurView frame and before adding blurView to weekndIV view
weekndIV.layer.cornerRadius = 10
weekndIV.layer.masksToBounds = true
I think you want to have a shadow effect on your view. For that you can access shadowOpacity and shadowRadius from layer of your view
For example in Swift 2:
class MyView : UIView {
override func awakeFromNib() {
layer.cornerRadius = 5.0
layer.shadowOpacity = 0.8
layer.shadowRadius = 5.0
layer.shadowOffset = CGSizeMake(2.0, 2.0)
}
}

Transparent blurred view is not working

I'd like to apply layer on top of ImageView that slightly blurrs it.
I put a view on top of imageView, subclass it as follows:
class BlurredView: UIView {
override func draw(_ rect: CGRect) {
self.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.extraLight)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.backgroundColor = UIColor.clear
blurEffectView.frame = self.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(blurEffectView)
}
}
The result is quite different from what I expected.
How to customize it to make this blur much lighter, so the picture still shows some details?
As https://stackoverflow.com/users/1387306/chrstpsln mentioned in the comment, this can be achieved by changing the alpha of the UIVisualEffectView.
As a bonus, you can add a fade animation to make the transition feel smoother.
Heres an example of how you can add this effect to any viewController through an extension:
extension UIViewController {
func addBlurEffect() {
let blurEffect = UIBlurEffect(style: .light)
let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
blurVisualEffectView.frame = UIScreen.main.bounds
blurVisualEffectView.alpha = 0.1
// Bonus animation - Or just set the alpha higher
UIView.animate(withDuration: 0.3) {
blurVisualEffectView.alpha = 0.90
}
view.addSubview(blurVisualEffectView)
}
}
Usage:
let viewController = UIViewController()
viewController.addBlurEffect()
You can't change the blur amount, the only thing you can do is to change the blur effect style:
UIBlurEffectStyle.light
UIBlurEffectStyle.extraLight
UIBlurEffectStyle.dark
Try to change the blur effect alpha. Also, since your image is light, you should use the light style and not the extra light in order to not lose details.
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
blurEffect.alpha = 0.95

Change color of the shadow in background

I`ve a App like the iOS 10 Maps-App or maybe like the Home-Screen with Control Center. For example take a look at the Picture. If you open the Control Center the shadow of the background changes to black & transparent. How can i access this layer or subview and change the color from this shadow to another in my App. Maybe to red or white?
EDIT:
Maybe directly with the Framework.
https://github.com/iosphere/ISHPullUp
You can use UIVisualEffectView like this:
Obj-C:
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];//SELECT STYLE OF YOUR CHOICE
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
blurEffectView.frame = self.view.bounds;//view Will be YOUR_VIEW
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:blurEffectView];//This add Blur view to your view
Swift:
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(blurEffectView)
Sometimes open your eyes and you will find what you want. 😕 If you using the same Framework there is one variable you can change. In the sample of the Framework you can got to the "BottomVC" and add the following line to your viewDidLoad().
weak var pullUpController: ISHPullUpViewController!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
topView.addGestureRecognizer(tapGesture)
handleView.arrowSize = CGSize(width: 1 , height: 2)
//This line change the default 40% black to a 70% white.
pullUpController.dimmingColor = UIColor.white().withAlphaComponent(0.7)
}
The "Shadow" is just a normal View. Here you can see how the author has implemented this.

Blur view with fade in edge effect - iOS

As you can see image below I want to create blur view with fade in edge effect. Does someone write me codes of how to implement this in ios?
Thanks
please take a look at this image - marked in red rectangle
let blurEffect = UIBlurEffect(style: .Light)
let imageView = UIImageView(image: UIImage(named:))//your ImageView
let visualEffectView = UIVisualEffectView(effect: blurEffect)
imageView.addSubview(visualEffectView)
You might need to change the frame of the visualEffectView to cover your image but this is the basics of it.

Resources