Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture UIImage of UIView including UINavigationBar

I found this code which is quite nice:

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

But I need to capture UINavigationBar as well because i want to use the image as transition layer in my UIStoryboardSegue. Any ideas?

like image 519
cschuff Avatar asked Jan 22 '12 11:01

cschuff


2 Answers

Use your view's window as the view so the navigation bar and status bar will be included, too. If you need to remove the status bar, you'll have to crop the image.

like image 55
Fabian Kreiser Avatar answered Nov 15 '22 15:11

Fabian Kreiser


You should take a screenshot of self.navigationController?.view . That way you get the whole view with navigationBar but without status bar.

if let view = self.navigationController?.view {
    let snapshot = view.snapshotViewAfterScreenUpdates(false)
}
like image 29
Aleš Oskar Kocur Avatar answered Nov 15 '22 16:11

Aleš Oskar Kocur