[iPad] the way to show contents to external display
I’m sorry for taking long time to post new entry.
Now I develop iPad app that shows my contents to external display.
I had many problems to implement it, so I post this article.
In addition, it is so hard to debug about external displaying because iPad cannot connect one plug.
Basically, the way to implement to show contents to external display:
1. get available screens to use [UIScreen screens] as NSArray.
2. get favorite UIScreenMode in 1.
3. get UIScreen.
4. set 2. to retrieved UIScreen object.
5. create contents that want to show to external display.
6. create UIWindow instance.
7. do [uiwindow addSubView:content];
8. set uiwindow.screen = 3.(UIScreen instance.)
9. set UIWindow basic configuration.
That’s all. Once you test it, it is easy.
Here is sample code:
——————————————
// log message displayed in external monitor.
NSMutableString *str = [[NSMutableString alloc] init];
int screenCount = [[UIScreen screens] count];
// used screen mode
UIScreenMode *current = nil;
// checking each screen information
for(int i = 0; i < screenCount; i++)
{
int modelen = [[[[UIScreen screens] objectAtIndex:i] availableModes] count];
// checking each screen mode in screen.
for (int j = 0; j < modelen; j++)
{
// getting screen mode
UIScreenMode *curMode = [[[[UIScreen screens] objectAtIndex:i] availableModes]
objectAtIndex:j];
// get information of screen.
NSString *sstr = [NSString stringWithFormat:@"screen:%i, mode:%i, w:%f, h:%f,
ratio:%f ---", i, j, current.size.width, current.size.height,
current.pixelAspectRatio];
[str appendString:sstr];
// if curMode.size.width is larger than current, change current reference
if(curMode.size.width > current.size.width){
current = curMode;
}
}
}
// screen object
UIScreen *another = [[UIScreen screens] objectAtIndex:1];
another.currentMode = current;
CGSize size = current.size;
uvc = [[UIViewController alloc] init];
// create new window.
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(
0.0f,
0.0f,
size.width,
size.height
)];
UITextView *tv = [[UITextView alloc] initWithFrame:CGRectMake(
0.0f, 0.0f, 800.0f, 800.0f)
];
[uvc.view addSubview:tv];
tv.text = str;
[tv release];
[window addSubview:uvc.view];
window.screen = another;
[window makeKeyAndVisible];
[window setHidden:NO];
—————————————————
BTW, this sample code is uploaded on github.
URL:
http://github.com/mmlemon/iPad-external-display-sample
If you have any question about this, please ask me.