I am trying to show a view modally. The view is shown as a "Cross Dissolve" "over full screen". I am passing the screen shot to the controller. I am then trying to crop the screenshot and retain only the part that would be under the view. This i am blurring and adding to the view.
The code works as far as blurring goes, but i have two problems. 1) The image is at double scale, which will be something to do with retina display, but i am never sure how to fix that. 2) the other is that, having tried everything I can think of, i cannot get the coordinate of the "canvas" in a coordinate system that helps me correctly crop the view.
I'd really appreciate help with this thanks karl
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let window = appDelegate.window!
let splitViewController = window.rootViewController as! UISplitViewController;
let cropFrame = UIView().convertRect(self.canvas.frame, toView: splitViewController.view)
let croppedScreenShotCG = CGImageCreateWithImageInRect(screenShot?.CGImage, cropFrame)
let croppedScreenShot = UIImage(CGImage:croppedScreenShotCG)
var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
var blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = CGRectMake(0, 0, self.canvas.frame.width, self.canvas.frame.height)
let blurImageView = UIImageView(image:croppedScreenShot)
blurImageView.addSubview(blurEffectView)
let blurColorCast = UIView(frame: CGRectMake(0, 0, self.canvas.frame.width, self.canvas.frame.height))
blurColorCast.backgroundColor = UIColor.cloverColor10pc()
blurColorCast.alpha = 0.2
blurImageView.addSubview(blurColorCast)
self.canvas.addSubview(blurImageView)
self.canvas.sendSubviewToBack(blurImageView)
UIImageWriteToSavedPhotosAlbum(croppedScreenShot, nil, nil, nil)
}
I am getting the screen shot like this:
let layer = window.layer
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Instead of using UIImage's CGImage:
you can use imageWithCGImage:scale:orientation:
which takes a scale parameter to account for retina screens.
But personally I prefer to avoid doing anything in code, if there's a reasonable way to do it with storyboards.
Here's an example project that has a blur effect for the back of a modal presentation that doesn't use any code at all.
The only thing worth noting is that for the view controller that's being presented, the root view background color is set to clear (otherwise the view controller that's presenting it would be obscured).