summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLScreenshotViewController.m
blob: 9ac5f4a8ce7365f957c3153d288aede2a0857b0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
//  SDLScreenShotViewController.m
//  ios
//
//  Created by Muller, Alexander (A.) on 2/6/17.
//  Copyright © 2017 Ford Motor Company. All rights reserved.
//

#import "SDLScreenshotViewController.h"

#import "SDLError.h"

@interface SDLScreenshotViewController ()

@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation SDLScreenshotViewController

- (instancetype)init {
    self = [super init];
    if (!self) { return nil; }

    self.view.backgroundColor = [UIColor clearColor];

    self.imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    self.imageView.backgroundColor = [UIColor clearColor];
    self.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.imageView];

    return self;
}

// HAX: https://github.com/smartdevicelink/sdl_ios/issues/1250
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    UIViewController *viewController = [self sdl_topMostControllerForWindow:[UIApplication sharedApplication].windows[0]];

    if (viewController == self) {
        return UIInterfaceOrientationMaskAll;
    } else if (viewController != nil) {
        return viewController.supportedInterfaceOrientations;
    }

    return UIInterfaceOrientationMaskAll;
}

// HAX: https://github.com/smartdevicelink/sdl_ios/issues/1250
- (BOOL)shouldAutorotate {
    UIViewController *viewController = [self sdl_topMostControllerForWindow:[UIApplication sharedApplication].windows[0]];

    if (viewController == self) {
        return YES;
    } else if (viewController != nil) {
        return viewController.shouldAutorotate;
    }

    return YES;
}

- (UIViewController *)sdl_topMostControllerForWindow:(UIWindow *)window {
    UIViewController *topController = window.rootViewController;

    while (topController.presentedViewController != nil) {
        topController = topController.presentedViewController;
    }

    return topController;
}

- (void)layoutSubviews {
    self.imageView.frame = self.view.bounds;
}

- (void)loadScreenshotOfWindow:(UIWindow *)window {
    UIGraphicsBeginImageContextWithOptions(window.bounds.size, YES, 0.0f);
    [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO];

    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.imageView.image = image;
}

@end