2020
2121#import " FBMacros.h"
2222#import " XCAXClient_iOS.h"
23- #import " XCUIScreen.h"
2423
2524static const NSTimeInterval FBHomeButtonCoolOffTime = 1 .;
2625
@@ -43,8 +42,8 @@ - (BOOL)fb_goToHomescreenWithError:(NSError **)error
4342
4443- (NSData *)fb_screenshotWithError : (NSError *__autoreleasing*)error
4544{
46- Class xcScreenClass = NSClassFromString (@" XCUIScreen" );
47- if (nil == xcScreenClass ) {
45+ id xcScreen = NSClassFromString (@" XCUIScreen" );
46+ if (nil == xcScreen ) {
4847 NSData *result = [[XCAXClient_iOS sharedClient ] screenshotData ];
4948 if (nil == result) {
5049 if (error) {
@@ -55,19 +54,27 @@ - (NSData *)fb_screenshotWithError:(NSError*__autoreleasing*)error
5554 return result;
5655 }
5756
57+ id mainScreen = [xcScreen valueForKey: @" mainScreen" ];
58+ CGSize screenSize = FBAdjustDimensionsForApplication (FBApplication.fb_activeApplication .frame .size , (UIInterfaceOrientation)[self .class sharedDevice ].orientation );
59+ SEL mSelector = NSSelectorFromString (@" screenshotDataForQuality:rect:error:" );
60+ NSMethodSignature *mSignature = [mainScreen methodSignatureForSelector: mSelector];
61+ NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: mSignature];
62+ [invocation setTarget: mainScreen];
63+ [invocation setSelector: mSelector];
5864 // https://developer.apple.com/documentation/xctest/xctimagequality?language=objc
5965 // Select lower quality, since XCTest crashes randomly if the maximum quality (zero value) is selected
6066 // and the resulting screenshot does not fit the memory buffer preallocated for it by the operating system
61- CGSize screenSize = FBAdjustDimensionsForApplication (FBApplication.fb_activeApplication .frame .size , (UIInterfaceOrientation)[self .class sharedDevice ].orientation );
6267 NSUInteger quality = 1 ;
68+ [invocation setArgument: &quality atIndex: 2 ];
6369 CGRect screenRect = CGRectMake (0 , 0 , screenSize.width , screenSize.height );
64-
65- XCUIScreen *mainScreen = (XCUIScreen *)[xcScreenClass mainScreen ];
66- NSData *result = [mainScreen screenshotDataForQuality: quality rect: screenRect error: error];
70+ [invocation setArgument: &screenRect atIndex: 3 ];
71+ [invocation setArgument: &error atIndex: 4 ];
72+ [invocation invoke ];
73+ NSData __unsafe_unretained *result;
74+ [invocation getReturnValue: &result];
6775 if (nil == result) {
6876 return nil ;
6977 }
70-
7178 // The resulting data is a JPEG image, so we need to convert it to PNG representation
7279 UIImage *image = [UIImage imageWithData: result];
7380 return (NSData *)UIImagePNGRepresentation (image);
0 commit comments