Testing on the iPhone 14 Pro simulator I notice if the device is in the landscape orientation, snapshot images my app takes are clipped (the x origin is off).
I take a snapshot of a view like this:
-(UIImage*)getSnapshotForView:(UIView*)snapShotView
{
UIGraphicsBeginImageContextWithOptions(snapShotView.bounds.size, NO, 0.0f);
[snapShotView drawViewHierarchyInRect:snapShotView.bounds afterScreenUpdates:YES];
UIImage *snapShotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapShotImage;
}
So I have an iPhone SE 3rd gen and all is working fine. But I figured I'd test in the iPhone 14 Pro simulator.
When the simulator is in landscape mode the generated image is inset like 100 points on the x origin and the content is clipped off on the right side.
So I looked at the view I was snapshotting and I thought I wasn't accounting for safe area insets properly (right and left) but I was. The subviews of the view I'm snapshotting all have the expected CGRect value for their frames.
Looks like iPhone 14 Pro is rendering snapshots location on the x axis when the device is in landscape mode (at least that appears to be the case in the simulator at least).
Any ideas if I'm doing something wrong?
Changing my snapshot code to use UIGraphicsImageRenderer appears to have fixed the issue:
UIGraphicsImageRenderer *imageRenderer = [[UIGraphicsImageRenderer alloc]initWithBounds:snapShotView.bounds];
UIImage *snapShotImage = [imageRenderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext)
{
CGContextRef cgContext = rendererContext.CGContext;
[snapShotView.layer renderInContext:cgContext];
}];
Not sure why UIGraphicsBeginImageContextWithOptions API was generating a clipped image only in the landscape orientation on certain iPhone models like iPhone 14 Pro. For whatever it's worth the view being "snapshotted" is offscreen (not in a UIWindow).
Migrating to the newer API works for me (I should have been using UIGraphicsImageRenderer in the first place anyway).