iPhone iPad上的iPhone应用程序:drawViewHierarchyInRect会导致故障



在iPad(iOS 8)上运行iPhone应用程序,使用drawviewHierarchyRect进行快照,并在两个视图控制器之间进行自定义动画。这一切都可以在iPhone上运行良好,但是当该应用在iPad上(不是通用应用程序,仅iPhone)时,您可以在动画启动时简要瞥见不良快照。基本上,它似乎需要整个iPad屏幕的快照,包括黑色边缘,而不仅仅是模拟的iPhone屏幕。该应用程序还隐藏了状态栏,但是您可以在简短的瞥见中看到状态栏,这意味着iOS中可能是某种错误,因为iPad始终在屏幕顶部显示iPhone应用程序的状态栏。

这是快照代码;

UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:update];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

顺便说一句,当使用AirPlay在Apple TV上显示该应用时,也会发生闪光灯。

用以下,

替换代码
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

事实证明,在这种情况下,似乎在某种程度上短暂地呈现到屏幕上的drawViewHierarchyInRect ...用Calayer's RenderinContext替换它可以消除故障,但不幸的是,我在iPhone上的性能较小,所以我添加确保iPad模拟是唯一需要执行此操作的支票(幸运的是它在iPad上足够快)

UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
if( [self iPadRunningiPhoneSize] )
{
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
}
else
{
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:update];
}
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

和检查iPhone应用程序在iPad上"模拟"的情况,以防该应用程序后来进行通用

- (BOOL)iPadRunningiPhoneSize
{
    int name[] = {CTL_HW,HW_MACHINE};
    size_t size = 100;
    sysctl(name, 2, NULL, &size, NULL, 0); // getting size of answer
    char *hw_machine = malloc(size);
    sysctl(name, 2, hw_machine, &size, NULL, 0);
    NSString *hardware = [NSString stringWithUTF8String:hw_machine];
    free(hw_machine);
    int greaterBound = MAX( [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height );
    return [hardware rangeOfString: @"iPad"].location != NSNotFound && greaterBound == 480;
}

使用iPad播放到Apple TV时,故障是固定的,但当然,检查模拟iPhone应用程序的iPad意味着使用AirPlay的iPhone仍然在电视上展示Glitch,但这并不是一个很好的关注点app!

最新更新