0

I was trying to capture and save an image of the splash screen inside application:didFinishLaunchingWithOptions: using the following code:

UIWindow *w = [[[UIApplication sharedApplication] windows] firstObject];

UIGraphicsBeginImageContext(w.bounds.size);
[w.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
NSString *storePath = [NSString stringWithFormat:@"%@/%ld.png", applicationDocumentsDir, unixTime];
[UIImagePNGRepresentation(img) writeToFile:storePath atomically:YES];

However unfortunately the image is saved as a transparent image. It is probably because my UIWindow object is not the right one. Can anybody please direct me into succeeding with that?

ishahak
  • 6,585
  • 5
  • 38
  • 56

2 Answers2

0

Why don't you try the below code after setting the Storyboard ID for the LaucnhScreen.storyboard file view controller.

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"LaunchScreen"
                                                             bundle: nil];

    UIViewController *controller = (UIViewController*)[mainStoryboard
                                                       instantiateViewControllerWithIdentifier: @"Launch"];
    UIGraphicsBeginImageContext(w.bounds.size);
    [controller.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
    NSString *storePath = [NSString stringWithFormat:@"%@/%ld.png", applicationDocumentsDir, unixTime];
    [UIImagePNGRepresentation(img) writeToFile:storePath atomically:YES];
Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
  • Thanks for the response but this does not answer my question. My splash screen is based on static images, not on a storyboard. I cannot change it for the project I'm working on – ishahak Jan 14 '17 at 19:29
0

Regarding of answer The UIApplicationDelegate usually has a reference to the "main window":

[[[UIApplication sharedApplication] delegate] window];
Community
  • 1
  • 1
miletliyusuf
  • 982
  • 1
  • 10
  • 12