Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mirror ViewController to External Display

I am trying to in code, get my view on the iPad app to mirror to an External Display. In AppDelegate didFinishLaunching, I have:

 if ([[UIScreen screens] count] > 1)
    {
        UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];
        NSString *availableModeString;

        for (int i = 0; i < secondScreen.availableModes.count; i++)
        {
            availableModeString = [NSString stringWithFormat:@"%f, %f",
                                   ((UIScreenMode *)[secondScreen.availableModes objectAtIndex:i]).size.width,
                                   ((UIScreenMode *)[secondScreen.availableModes objectAtIndex:i]).size.height];

            [[[UIAlertView alloc] initWithTitle:@"Available Mode" message:availableModeString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
            availableModeString = nil;
        }

        // undocumented value 3 means no overscan compensation
        secondScreen.overscanCompensation = 3;

        self.secondWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 1280, 720)];
        self.secondWindow.backgroundColor = [UIColor blueColor];
        self.secondWindow.screen = secondScreen;

        ViewController *viewController = [[ViewController alloc] init];
        self.secondWindow.rootViewController = viewController;

        self.secondWindow.hidden = NO;
    }

All that shows up in the external display is the blue background color set in code.

like image 475
user717452 Avatar asked Sep 24 '26 16:09

user717452


1 Answers

I can mirror a view on my iPhone 6 using this:

@interface MirrorViewController ()

@property (nonatomic, retain) UIView *viewToMirror;
@property (nonatomic, retain) UIView *snapshotView;

@end

@implementation MirrorViewController

- (instancetype)initWithViewToMirror:(UIView*)view
{
    self = [super initWithNibName:nil bundle:nil];
    if (self == nil) return nil;
    self.viewToMirror = view;
    return self;
}

- (void)viewDidLoad
{
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)update
{
    [self.snapshotView removeFromSuperview];
    self.snapshotView = [self.viewToMirror snapshotViewAfterScreenUpdates:NO];
    [self.view addSubview:self.snapshotView];
}

@end

Here's how you could use it in your didFinishLaunching code:

if ([[UIScreen screens] count] > 1)
{
    UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];

    // [...]

    self.secondWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 1280, 720)];
    self.secondWindow.backgroundColor = [UIColor blueColor];
    self.secondWindow.screen = secondScreen;

    MirrorViewController *mirrorViewController = [[MirrorViewController alloc] initWithViewToMirror:self.window.rootViewController.view];
    self.secondWindow.rootViewController = mirrorViewController;

    self.secondWindow.hidden = NO;
}

I haven't tested it on an iPad with external display though because i don't have the gear.

like image 77
quad16 Avatar answered Sep 27 '26 06:09

quad16



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!