6

HI there,

I'm trying to use UIScreen to drive a separate screen with the VGA dongle on my iPad.

Here's what I've got in my root view controller's viewDidLoad:

//Code to detect if an external display is connected to the iPad.
 NSLog(@"Number of screens: %d", [[UIScreen screens]count]);

 //Now, if there's an external screen, we need to find its modes, itereate through them and find the highest one. Once we have that mode, break out, and set the UIWindow.

 if([[UIScreen screens]count] > 1) //if there are more than 1 screens connected to the device
 {
  CGSize max;
  UIScreenMode *maxScreenMode;
  for(int i = 0; i < [[[[UIScreen screens] objectAtIndex:1] availableModes]count]; i++)
  {
   UIScreenMode *current = [[[[UIScreen screens]objectAtIndex:1]availableModes]objectAtIndex:i];
   if(current.size.width > max.width);
   {
    max = current.size;
    maxScreenMode = current;
   }
  }
  //Now we have the highest mode. Turn the external display to use that mode.
  UIScreen *external = [[UIScreen screens] objectAtIndex:1];
  external.currentMode = maxScreenMode;
  //Boom! Now the external display is set to the proper mode. We need to now set the screen of a new UIWindow to the external screen
  external_disp = [externalDisplay alloc];
  external_disp.drawImage = drawViewController.drawImage;
  UIWindow *newwindow = [UIWindow alloc];
  [newwindow addSubview:external_disp.view];
  newwindow.screen = external;
 }
Peter Hajas
  • 3,861
  • 4
  • 23
  • 27
  • 1
    What's so iPad specific about this? Can you output to an external display on iPhone also? – Ali Nov 29 '10 at 09:55

6 Answers6

8

You need to init your window...

 UIWindow *newwindow = [[UIWindow alloc] init];
Hiren
  • 12,525
  • 7
  • 51
  • 71
Det Ansinn
  • 96
  • 1
2

[newwindow makeKeyAndVisible];?

Noah Witherspoon
  • 56,163
  • 16
  • 128
  • 131
  • 1
    I added this right after the last line, and it still seems to do the same thing. It's interesting, because I can tell (using my monitor) when the VGA display has been "claimed" by the app, it's the same thing that happens when Keynote is launched; the display won't toggle back to DVI mode or go to sleep. Could there be a problem with my externalDisplay view controller? It's pretty benign, just a .xib file with a UIImageView, nothing complex. Thanks for the suggestion! – Peter Hajas Apr 17 '10 at 23:45
  • I now have a view shown on the VGA output Unfortunately, any change to the drawImage parameter of externalDisplay seems to not work. I have added other IBOutlets which work, but not drawImage (which is a UIImageView). Any ideas? – Peter Hajas Apr 18 '10 at 06:22
  • Thanks Noah - this actually was part of my problem, but I forgot to initialize the window also. Thanks for your response! – Peter Hajas Apr 20 '10 at 18:32
2

I think your problem is the externalDisplay. Create a viewcontroller outside your code (maybe do a simply Add new file ViewController and put stuff in the .xib) and try it out to make sure that viewcontroller is working before you call it into the external display. Here's your code with my suggested changes -- [mainViewController view] is the viewcontroller you created outside.

//Code to detect if an external display is connected to the iPad.
NSLog(@"Number of screens: %d", [[UIScreen screens]count]);

//Now, if there's an external screen, we need to find its modes, iterate
//through them and find the highest one. Once we have that mode, break out,
//and set the UIWindow.

if([[UIScreen screens]count] > 1) //if there are more than 1 screens connected
                                  //to the device
{
 CGSize max;
 UIScreenMode *maxScreenMode;
 for(int i = 0; i < [[[[UIScreen screens] objectAtIndex:1] availableModes]count]; i++)
 {
  UIScreenMode *current = [[[[UIScreen screens]objectAtIndex:1]availableModes]objectAtIndex:i];
  if(current.size.width > max.width);
  {
   max = current.size;
   maxScreenMode = current;
  }
 }
 //Now we have the highest mode. Turn the external display to use that mode.
 UIScreen *external = [[UIScreen screens] objectAtIndex:1];
 external.currentMode = maxScreenMode;
 //Boom! Now the external display is set to the proper mode. We need to now
 //set the screen of a new UIWindow to the external screen

 UIWindow *newwindow = [UIWindow alloc];

 [newwindow addSubview:[mainViewController view]];
 newwindow.screen = external;

 [newwindow makeKeyAndVisible];
 [newwindow setHidden:NO];
}
sth
  • 200,334
  • 49
  • 262
  • 354
vip malixi
  • 21
  • 2
2

Just recording this here in case anyone stumbles on this question. I wasn't able to get anything to show up on the second screen until I realized that my app delegate had to retain the UIWindow. It has no natural owner, so if you just do a regular autorelease, the window will be released before it ever displays.

Hope that helps.

Bill
  • 38,492
  • 24
  • 114
  • 205
  • 1
    Working on something similar I had that problem too, thanks! I'm using ARC and wasn't even considering who would be retaining it. – jblocksom Jan 04 '13 at 03:30
1

I've uploaded sample .xcodeproj into github.

I refered to this page mainly.

Thanks a lot. :)

http://github.com/igaiga/iPadDisplayOutSample

igaiga
  • 11
  • 2
0

It needs to be mentioned that the code provided on this page and on the github link by igaiga is merely meant to "move" (NOT CLONE) the view that would normally be on the iPad (or other device).

If you need to clone (aka Mirror) the view and refresh its contents this link is more suitable: http://www.touchcentric.com/blog/archives/123

I hope this helps to clarify the use cases for both sets of code for users just starting to integrate video out capabilities into existing apps.

Mike
  • 1
  • 1