Skip to content

Commit

Permalink
Merge pull request CoderZhuXH#196 from devcxm/master
Browse files Browse the repository at this point in the history
解决LaunchScreen子视图使用Safe Area布局后,在截图时的子视图位置偏移的问题
  • Loading branch information
CoderZhuXH authored Jan 9, 2020
2 parents f91327d + 7de2e55 commit 4c7c4f9
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion XHLaunchAd/XHLaunchAd/XHLaunchImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ -(UIImage *)imageFromLaunchScreen{
UIViewController *LaunchScreenSb = [[UIStoryboard storyboardWithName:UILaunchStoryboardName bundle:nil] instantiateInitialViewController];
if(LaunchScreenSb){
UIView * view = LaunchScreenSb.view;
view.frame = [UIScreen mainScreen].bounds;

// 加入到UIWindow后,LaunchScreenSb.view的safeAreaInsets在刘海屏机型才正常。
UIWindow *containerWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
view.frame = containerWindow.bounds;
[containerWindow addSubview:view];
[containerWindow layoutIfNeeded];

UIImage *image = [self imageFromView:view];
return image;
}
Expand All @@ -68,6 +74,7 @@ -(UIImage*)imageFromView:(UIView*)view{
//参数1:表示区域大小 参数2:如果需要显示半透明效果,需要传NO,否则传YES 参数3:屏幕密度
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
Expand Down

0 comments on commit 4c7c4f9

Please sign in to comment.