views:

3311

answers:

9

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;
 }
+2  A: 

[newwindow makeKeyAndVisible];?

Noah Witherspoon
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
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
Thanks Noah - this actually was part of my problem, but I forgot to initialize the window also. Thanks for your response!
Peter Hajas
+8  A: 

You need to init your window...

UIWindow *newwindow = [[UIWindow alloc] init];

Det Ansinn
Thanks so much for this! This did it!I can't believe I forgot...
Peter Hajas
A: 

Did anybody manage to solve this problem?

All I see is a black screen at the projector.

Thanks.

Ed
What's your code for initializing the external window? I can help you out.
Peter Hajas
A: 

Can you post ALL the code? I have only what you posted and I am getting errors.

Thanks.

Blake
When I get back to my machine, I'll post a code example and maybe a .xcodeproj demo for people on github.
Peter Hajas
A: 

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];
}
vip malixi
+1  A: 

I've uploaded sample .xcodeproj into github.

I refered to this page mainly.

Thanks a lot. :)

http://github.com/igaiga/iPadDisplayOutSample

igaiga
A: 

Sorry to add a Question on here, but is it also possible to reverse this so that removing the external display doesn't kill the application?

Alan Taylor
A: 

It seems that the touches interaction don't work when on the external monitor. Have you noticed that?

How can I rotate the screen? When the app runs on the iPad it is on Landscape mode , but when the external monitor the app runs on Portrait mode.

Alex