iOS截取LaunchScreen.storyboard图片

代码

UIViewController * vc = [[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
CGRect rect = [UIScreen mainScreen].bounds;
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
vc.view.frame = rect;
[vc.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

注意事项

  1. storyboard的背景图片设置上下约束的时候的对象是superView,而不是Top Layout Guidebottom Layout Guide
  2. 设置约束的时候取消Constrain to margins
  3. vc.view.frame = rect;这里要根据屏幕大小设置一下view的大小,因为如果storyboard是根据iPhone8的屏幕制作的,那么此时view的大小无法满足plus以及全面屏

你可能感兴趣的:(iOS截取LaunchScreen.storyboard图片)