Snapshot methods broken on iPhone 6 device and sim

2020-07-01 07:16发布

问题:

For some unknown reason there seems to be a possible bug with all screenshot methods on the iPhone 6 simulator (and device). Whenever I call any of the screenshot methods including:

snapshotViewAfterScreenUpdates: resizableSnapshotViewFromRect: drawViewHierarchyInRect:

with afterScreenUpdates set to YES, the screen flickers. If set to NO, then no flicker occurs, but I am unable to get the functionality that I need.

These methods work fine with both iOS7.1 and iOS8 in all other simulators except for the iPhone 6 and 6+.

Strangely enough, if I start a brand new project using storyboards and try similar code I can't reproduce the flicker. I have attached a gif of the flicker using my non-storyboard project:

And here is the very simple view controller:

    @implementation TestSnapshotController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Snap" style:UIBarButtonItemStylePlain target:self action:@selector(_snap)];

    self.blueView = [UIView new];
    self.blueView.backgroundColor = [UIColor blueColor];
    self.blueView.frame = CGRectMake(100.0f, 100.0f, 100.0f, 100.0f);
    [self.view addSubview:self.blueView];
}

- (void)_snap
{
    [self.blueView snapshotViewAfterScreenUpdates:YES];
}

@end

And here is my AppDelegate just in case:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    TestSnapshotController *testVC = [TestSnapshotController new];
    UINavigationController *rootNavVC = [[UINavigationController alloc] initWithRootViewController:testVC];

    self.window.rootViewController = rootNavVC;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

Any help would be greatly appreciated!

回答1:

We are seeing this same issue when running the following code:

[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];

The only solution that we have found so far is to make sure that the application has launch images sized for the iPhone 6 and 6+ and then we no longer are getting the flickering.



回答2:

Following on from Ryans solution, I added a launch screen xib (or storyboard) and this resolved the issue on the iPhone 6 and 6 plus.

So make sure you have it set in your project settings, it should end up looking something like this:



回答3:

It seems to be a bug on Apple's side that occurs on scaled apps (those that have no design/assets/layout support for iPhone 6/6+) This one helped me get over it: snapshotViewAfterScreenUpdates glitch on iOS 8

Basically uses: [view.layer renderInContext:UIGraphicsGetCurrentContext()];