2010-04-17 45 views
6

HI there,使用UIScreen驅動VGA顯示器 - 似乎不顯示UIWindow?

我試圖使用UIScreen在iPad上使用VGA加密狗驅動單獨的屏幕。

這裏就是我有我的根視圖控制器的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; 
} 
+1

這是什麼iPad具體這個?你能輸出到iPhone上的外部顯示器嗎? – Ali 2010-11-29 09:55:49

回答

8

你需要初始化你的窗口...

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

非常感謝!這做到了! 我不能相信我忘了... – 2010-04-20 18:31:35

2

[newwindow makeKeyAndVisible];

+1

我在最後一行之後添加了它,它似乎仍然做同樣的事情。 這很有趣,因爲我可以告訴(使用我的顯示器)何時VGA顯示器已被應用程序「聲稱」,這與Keynote啓動時發生的情況是一樣的;顯示屏將不會切換回DVI模式或進入睡眠模式。 我的externalDisplay視圖控制器可能有問題嗎?這非常好,只是一個帶有UIImageView的.xib文件,沒有什麼複雜的。 感謝您的建議! – 2010-04-17 23:45:11

+0

我現在在VGA輸出上顯示一個視圖不幸的是,對externalDisplay的drawImage參數的任何更改似乎都不起作用。我添加了其他工作的IBOutlets,但不包含drawImage(這是一個UIImageView)。有任何想法嗎? – 2010-04-18 06:22:47

+0

感謝諾亞 - 這實際上是我的問題的一部分,但我忘了初始化窗口。感謝您的答覆! – 2010-04-20 18:32:07

2

我覺得你的問題是externalDisplay。在你的代碼之外創建一個viewcontroller(也許做一個簡單的添加新文件ViewController並把東西放在.xib中),然後在將它調用到外部顯示之前確保viewcontroller正在工作。這是你的代碼和我的建議更改 - [mainViewController視圖]是您在外部創建的視圖控制器。

//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]; 
} 
0

它需要提及的是,提供了這個網頁上,並igaiga GitHub的鏈接代碼僅僅意味着「移動」(不能克隆),通常會在iPad上的觀點(或其他設備)。

如果您需要克隆(又名鏡像)的觀點,並刷新其內容這一環節更適合:http://www.touchcentric.com/blog/archives/123

我希望這有助於澄清這兩套代碼,爲用戶的使用情況剛剛開始整合將視頻輸出到現有應用程序中。

2

只是在這裏記錄這個以防止任何人在這個問題上絆倒。在我意識到我的應用程序委託必須保留UIWindow之前,我無法在第二個屏幕上顯示任何內容。它沒有自然的所有者,所以如果你只是做一個常規的autorelease,窗口會在它顯示之前被釋放。

希望有所幫助。

+1

工作類似的東西我也有這個問題,謝謝!我正在使用ARC,甚至沒有考慮誰會保留它。 – jblocksom 2013-01-04 03:30:49

相關問題