@@ -153,6 +153,18 @@ - (NSData *)fb_screenshotWithError:(NSError **)error
153153 NSUInteger quality = 1 ;
154154 [invocation setArgument: &quality atIndex: 2 ];
155155 CGRect elementRect = self.frame ;
156+ UIInterfaceOrientation orientation = self.application .interfaceOrientation ;
157+ if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
158+ // Workaround XCTest bug when element frame is returned as in portrait mode even if the screenshot is rotated
159+ XCElementSnapshot *parentWindow = [self .fb_lastSnapshot fb_parentMatchingType: XCUIElementTypeWindow];
160+ CGRect appFrame = self.application .frame ;
161+ if (CGRectEqualToRect (appFrame, nil == parentWindow ? elementRect : parentWindow.frame )) {
162+ CGPoint fixedOrigin = orientation == UIInterfaceOrientationLandscapeLeft ?
163+ CGPointMake (appFrame.size .height - elementRect.origin .y - elementRect.size .height , elementRect.origin .x ) :
164+ CGPointMake (elementRect.origin .y , appFrame.size .width - elementRect.origin .x - elementRect.size .width );
165+ elementRect = CGRectMake (fixedOrigin.x , fixedOrigin.y , elementRect.size .height , elementRect.size .width );
166+ }
167+ }
156168 [invocation setArgument: &elementRect atIndex: 3 ];
157169 [invocation setArgument: &error atIndex: 4 ];
158170 [invocation invoke ];
@@ -161,26 +173,7 @@ - (NSData *)fb_screenshotWithError:(NSError **)error
161173 if (nil == imageData) {
162174 return nil ;
163175 }
164-
165- UIImage *image = [UIImage imageWithData: imageData];
166- UIInterfaceOrientation orientation = self.application .interfaceOrientation ;
167- UIImageOrientation imageOrientation = UIImageOrientationUp;
168- // The received element screenshot will be rotated, if the current interface orientation differs from portrait, so we need to fix that first
169- if (orientation == UIInterfaceOrientationLandscapeRight) {
170- imageOrientation = UIImageOrientationLeft;
171- } else if (orientation == UIInterfaceOrientationLandscapeLeft) {
172- imageOrientation = UIImageOrientationRight;
173- } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
174- imageOrientation = UIImageOrientationDown;
175- }
176- CGSize size = image.size ;
177- UIGraphicsBeginImageContext (CGSizeMake (size.width , size.height ));
178- [[UIImage imageWithCGImage: (CGImageRef)[image CGImage ] scale: 1.0 orientation: imageOrientation] drawInRect: CGRectMake (0 , 0 , size.width, size.height)];
179- UIImage *fixedImage = UIGraphicsGetImageFromCurrentImageContext ();
180- UIGraphicsEndImageContext ();
181-
182- // The resulting data is a JPEG image, so we need to convert it to PNG representation
183- return (NSData *)UIImagePNGRepresentation (fixedImage);
176+ return FBAdjustScreenshotOrientationForApplication (imageData, orientation);
184177}
185178
186179@end
0 commit comments