diff --git a/src/plugins/platforms/ios/qiosscreen.mm b/src/plugins/platforms/ios/qiosscreen.mm index 1e0d074330a..ad735266e03 100644 --- a/src/plugins/platforms/ios/qiosscreen.mm +++ b/src/plugins/platforms/ios/qiosscreen.mm @@ -428,19 +428,33 @@ QPixmap QIOSScreen::grabWindow(WId window, int x, int y, int width, int height) CGRect captureRect = [view.window convertRect:CGRectMake(x, y, width, height) fromView:view]; captureRect = CGRectIntersection(captureRect, view.window.bounds); - UIGraphicsBeginImageContextWithOptions(captureRect.size, NO, 0.0); - CGContextRef context = UIGraphicsGetCurrentContext(); - CGContextTranslateCTM(context, -captureRect.origin.x, -captureRect.origin.y); + QMacAutoReleasePool autoReleasePool; - // Draws the complete view hierarchy of view.window into the given rect, which - // needs to be the same aspect ratio as the view.window's size. Since we've - // translated the graphics context, and are potentially drawing into a smaller - // context than the full window, the resulting image will be a subsection of the - // full screen. - [view.window drawViewHierarchyInRect:view.window.bounds afterScreenUpdates:NO]; + UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat]; + format.opaque = NO; + format.scale = devicePixelRatio(); + // Could be adjusted to support HDR in the future. + format.preferredRange = UIGraphicsImageRendererFormatRangeStandard; - UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + UIGraphicsImageRenderer *renderer = [[[UIGraphicsImageRenderer alloc] + initWithSize:captureRect.size format:format] + autorelease]; + + UIImage *screenshot = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) { + CGContextRef context = rendererContext.CGContext; + CGContextTranslateCTM(context, -captureRect.origin.x, -captureRect.origin.y); + + // Draws the complete view hierarchy of view.window into the given rect, which + // needs to be the same aspect ratio as the view.window's size. Since we've + // translated the graphics context, and are potentially drawing into a smaller + // context than the full window, the resulting image will be a subsection of the + // full screen. + // + // TODO: Should only be run on the UI thread in the future. At + // the time of writing, QScreen::grabWindow doesn't include any + // requirements as to which thread it can be called from. + [view.window drawViewHierarchyInRect:view.window.bounds afterScreenUpdates:NO]; + }]; return QPixmap::fromImage(qt_mac_toQImage(screenshot.CGImage)); }