I tried below steps to capture screenshot.
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions) {
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
}
else
{
UIGraphicsBeginImageContext(imageSize);
}
CGContextRef context = UIGraphicsGetCurrentContext();
@try {
for (UIWindow * window in [[UIApplication sharedApplication] windows]) {
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, [window center].x, [window center].y);
CGContextConcatCTM(context, [window transform]);
CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);
dispatch_sync(dispatch_get_main_queue(), ^{
LogInfo(@"before renderInContext on main thread");
[[window layer] renderInContext:context];
LogInfo(@"after renderInContext on main thread");
});
CGContextRestoreGState(context);
}
}
}
@catch (NSException *exception) {
LogInfo(@"catch NSException %@ in captureScreen",[exception name]);
}
@finally {
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
But sometimes crash, Crash Log:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x11bd70d8
Triggered by Thread: 0
Thread 0 Crashed:
0 CoreGraphics 0x2f9829ce argb32_image_mark_argb32 + 402
1 CoreGraphics 0x2f9827d2 argb32_image_mark_image + 706
2 CoreGraphics 0x2f97e126 argb32_image + 2106
3 libRIP.A.dylib 0x2fccbafa ripl_Mark + 14
4 libRIP.A.dylib 0x2fcd61ac RIPLayerBltImage + 728
5 libRIP.A.dylib 0x2fcc9862 ripc_RenderImage + 434
6 libRIP.A.dylib 0x2fcc816e ripc_DrawImage + 662
7 CoreGraphics 0x2f97b6f8 CGContextDelegateDrawImage + 48
8 CoreGraphics 0x2f97b57c CGContextDrawImage + 280
9 QuartzCore 0x31dfa642 -[CALayer _renderForegroundInContext:] + 2266
10 QuartzCore 0x31df976a -[CALayer renderInContext:] + 1222
11 QuartzCore 0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
12 QuartzCore 0x31df9782 -[CALayer renderInContext:] + 1246
13 QuartzCore 0x31df98b4 -[CALayer renderInContext:] + 1552
14 QuartzCore 0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
15 QuartzCore 0x31df9782 -[CALayer renderInContext:] + 1246
16 UIKit 0x32721322 -[_UIBackdropViewLayer renderInContext:] + 78
17 QuartzCore 0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
18 QuartzCore 0x31df9782 -[CALayer renderInContext:] + 1246
19 QuartzCore 0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
20 QuartzCore 0x31df9782 -[CALayer renderInContext:] + 1246
21 QuartzCore 0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
22 QuartzCore 0x31df9782 -[CALayer renderInContext:] + 1246
23 QuartzCore 0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
24 QuartzCore 0x31df9782 -[CALayer renderInContext:] + 1246
25 RuiKlasse 0x00113598 0x8e000 + 546200
26 libdispatch.dylib 0x3a8cbb36 _dispatch_barrier_sync_f_slow_invoke + 66
27 libdispatch.dylib 0x3a8c5d3c _dispatch_client_callout + 20
28 libdispatch.dylib 0x3a8c86be _dispatch_main_queue_callback_4CF + 274
29 CoreFoundation 0x2f8cc674 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 4
30 CoreFoundation 0x2f8caf40 __CFRunLoopRun + 1304
31 CoreFoundation 0x2f8357a4 CFRunLoopRunSpecific + 520
32 CoreFoundation 0x2f835586 CFRunLoopRunInMode + 102
33 GraphicsServices 0x3476f6ce GSEventRunModal + 134
34 UIKit 0x3219488c UIApplicationMain + 1132
35 RuiKlasse 0x0024d46c 0x8e000 + 1832044
36 libdyld.dylib 0x3a8daab4 start + 0
This issue doesn't happen every time, but it happens just enough to render my app unusable(crashed).I'm at my wit's end with this. Could anyone know how to fix it or give me some hints to investigate the problem?
further findings:
1.even though I run the entire code on main thread, this problem still happened. 2.KERN_INVALID_ADDRESS doesn't point to context. (I got the address of context and found that they were different)
Am I doing something wrong? How did you solved it?