外部屏幕快照强行进入按钮背景

时间:2014-07-01 10:17:05

标签: ios objective-c ipad

我的应用程序使用外部屏幕,因此在我的iPad应用程序中,我的控制器很像外部屏幕的遥控器:

enter image description here

你可以看到RemoteControlViewController本身,它有: 1)关闭按钮 2)动态可添加按钮(它们用于更改外部屏幕上显示的内容)。 3)外部屏幕快照,以便用户知道外部屏幕上当前的内容。

在viewLoad(我的RemoteControlViewController)上,我拍摄外部屏幕的快照并将其放在UIImageView上 但是在第二次打开RemoteControl时 - 按钮背景充满了外部屏幕快照 - 如何防止这种情况?

- (void)viewDidLoad
{
[super viewDidLoad];

//Add external display controls
[self addExternalDisplayControls];

//Display initial image
[self getExternalDisplaySnapshot];

//Add notification listeners to show/hide external display miniature responsivly
[self setUpScreenConnectionNotificationHandlers];
}

-(void)getExternalDisplaySnapshot
{
//Load screenshot
if ([[UIScreen screens] count] > 1)
{
    self.iboExternalDisplayNotFoundMessage.text = @"";
    self.iboScaledExternalDisplay.hidden=NO;
    self.iboExternalDisplayControlsPanel.hidden=NO;
    // Get the screen object that represents the external display.
    UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];

    //Detect if retina
    if ([secondScreen respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(secondScreen.bounds.size, NO, secondScreen.scale);
    else
        UIGraphicsBeginImageContext(secondScreen.bounds.size);
    //Get external windows instance from AppDelegate
    UIWindow   *externalWindow =[(AppDelegate*)[[UIApplication sharedApplication] delegate] secondWindow];
    //Render the screen
    [externalWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //Set image to imageview
    self.iboScreenImage.image = image;
}
else
{
    self.iboExternalDisplayNotFoundMessage.text = @"No External Displays are connected.";
    self.iboScaledExternalDisplay.hidden=YES;
    self.iboExternalDisplayControlsPanel.hidden=YES;
}
}

- (void)setUpScreenConnectionNotificationHandlers
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserver:self selector:@selector(handleScreenDidConnectNotification:)
               name:UIScreenDidConnectNotification object:nil];
[center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:)
               name:UIScreenDidDisconnectNotification object:nil];
}

- (void)handleScreenDidConnectNotification:(NSNotification*)aNotification
{
//Just show the screen miniature
[self getExternalDisplaySnapshot];
}

- (void)handleScreenDidDisconnectNotification:(NSNotification*)aNotification
{
//Just show the screen miniature
[self getExternalDisplaySnapshot];
}

问题是,当我关闭控制器并再次调用它时 - 按钮背景有外部屏幕快照的图像!为什么?如何防止这种情况?

这是我添加按钮的代码:

-(void)addExternalDisplayControls{
//Adds blood products button
CGRect rect = CGRectMake(10, 5, 100, 50);
UIButton *button = [self createButton:@"button" cords:rect buttonTag:0];
[self.iboExternalDisplayControlsPanel addSubview:button];
}

- (UIButton *)createButton:(NSString *)title cords:(CGRect)cords buttonTag:(int)tag {
UIButtonStyled *button = [UIButtonStyled new];
button.tag=tag;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setTitle:title forState:UIControlStateNormal];
[button setStyleName:@"buttonSelection"];
//button.styleName = @"buttonSelection";
button.titleLabel.numberOfLines = 2;
[button addTarget:self action:@selector(p_buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.frame = cords;
return button;
}

1 个答案:

答案 0 :(得分:-1)

我真的不知道这件事是怎么发生的以及它是如何发生的,但在我的快照拍摄功能结束时我添加了这个:

UIGraphicsEndImageContext();

所有作品现在都像是一种魅力。

这是快照功能:

// Get the screen object that represents the external display.
    UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];

    //Detect if retina
    if ([secondScreen respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(secondScreen.bounds.size, NO, secondScreen.scale);
    else
        UIGraphicsBeginImageContext(secondScreen.bounds.size);
    //Get external windows instance from AppDelegate
    UIWindow   *externalWindow =[(AppDelegate*)[[UIApplication sharedApplication] delegate] secondWindow];
    //Render the screen
    [externalWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //Set image to imageview
    self.iboScreenImage.image = image;
    UIGraphicsEndImageContext();