2020
2121#import " FBMacros.h"
2222#import " XCAXClient_iOS.h"
23+ #import " XCUIScreen.h"
2324
2425static const NSTimeInterval FBHomeButtonCoolOffTime = 1 .;
2526
@@ -42,8 +43,8 @@ - (BOOL)fb_goToHomescreenWithError:(NSError **)error
4243
4344- (NSData *)fb_screenshotWithError : (NSError *__autoreleasing*)error
4445{
45- id xcScreen = NSClassFromString (@" XCUIScreen" );
46- if (nil == xcScreen ) {
46+ Class xcScreenClass = NSClassFromString (@" XCUIScreen" );
47+ if (nil == xcScreenClass ) {
4748 NSData *result = [[XCAXClient_iOS sharedClient ] screenshotData ];
4849 if (nil == result) {
4950 if (error) {
@@ -53,29 +54,20 @@ - (NSData *)fb_screenshotWithError:(NSError*__autoreleasing*)error
5354 }
5455 return result;
5556 }
56-
57- id mainScreen = [xcScreen valueForKey: @" mainScreen" ];
58- XCUIApplication *app = FBApplication.fb_activeApplication ;
59- CGSize screenSize = FBAdjustDimensionsForApplication (app.frame .size , app.interfaceOrientation );
60- SEL mSelector = NSSelectorFromString (@" screenshotDataForQuality:rect:error:" );
61- NSMethodSignature *mSignature = [mainScreen methodSignatureForSelector: mSelector];
62- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: mSignature];
63- [invocation setTarget: mainScreen];
64- [invocation setSelector: mSelector];
57+
6558 // https://developer.apple.com/documentation/xctest/xctimagequality?language=objc
6659 // Select lower quality, since XCTest crashes randomly if the maximum quality (zero value) is selected
6760 // 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 );
6862 NSUInteger quality = 1 ;
69- [invocation setArgument: &quality atIndex: 2 ];
7063 CGRect screenRect = CGRectMake (0 , 0 , screenSize.width , screenSize.height );
71- [invocation setArgument: &screenRect atIndex: 3 ];
72- [invocation setArgument: &error atIndex: 4 ];
73- [invocation invoke ];
74- NSData __unsafe_unretained *result;
75- [invocation getReturnValue: &result];
64+
65+ XCUIScreen *mainScreen = (XCUIScreen *)[xcScreenClass mainScreen ];
66+ NSData *result = [mainScreen screenshotDataForQuality: quality rect: screenRect error: error];
7667 if (nil == result) {
7768 return nil ;
7869 }
70+
7971 // The resulting data is a JPEG image, so we need to convert it to PNG representation
8072 UIImage *image = [UIImage imageWithData: result];
8173 return (NSData *)UIImagePNGRepresentation (image);
0 commit comments