summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/gui/kernel/qscreen.cpp4
-rw-r--r--src/plugins/platforms/ios/qiosscreen.h2
-rw-r--r--src/plugins/platforms/ios/qiosscreen.mm33
3 files changed, 39 insertions, 0 deletions
diff --git a/src/gui/kernel/qscreen.cpp b/src/gui/kernel/qscreen.cpp
index bc4a25a65f..1dd8fb5e67 100644
--- a/src/gui/kernel/qscreen.cpp
+++ b/src/gui/kernel/qscreen.cpp
@@ -667,6 +667,10 @@ void QScreenPrivate::updatePrimaryOrientation()
that are not part of the application, window system frames, and so
on.
+ \warning Grabbing windows that are not part of the application is
+ not supported on systems such as iOS, where sandboxing/security
+ prevents reading pixels of windows not owned by the application.
+
The grabWindow() function grabs pixels from the screen, not from
the window, i.e. if there is another window partially or entirely
over the one you grab, you get pixels from the overlying window,
diff --git a/src/plugins/platforms/ios/qiosscreen.h b/src/plugins/platforms/ios/qiosscreen.h
index cc83e7f3d2..9fcce42825 100644
--- a/src/plugins/platforms/ios/qiosscreen.h
+++ b/src/plugins/platforms/ios/qiosscreen.h
@@ -68,6 +68,8 @@ public:
Qt::ScreenOrientation orientation() const Q_DECL_OVERRIDE;
void setOrientationUpdateMask(Qt::ScreenOrientations mask) Q_DECL_OVERRIDE;
+ QPixmap grabWindow(WId window, int x, int y, int width, int height) const override;
+
UIScreen *uiScreen() const;
UIWindow *uiWindow() const;
diff --git a/src/plugins/platforms/ios/qiosscreen.mm b/src/plugins/platforms/ios/qiosscreen.mm
index b9c77e9bba..bd2aa0e7e7 100644
--- a/src/plugins/platforms/ios/qiosscreen.mm
+++ b/src/plugins/platforms/ios/qiosscreen.mm
@@ -47,6 +47,7 @@
#include "quiview.h"
#include <QtGui/private/qwindow_p.h>
+#include <private/qcoregraphics_p.h>
#include <sys/sysctl.h>
@@ -457,6 +458,38 @@ void QIOSScreen::setOrientationUpdateMask(Qt::ScreenOrientations mask)
}
}
+QPixmap QIOSScreen::grabWindow(WId window, int x, int y, int width, int height) const
+{
+ if (window && ![reinterpret_cast<id>(window) isKindOfClass:[UIView class]])
+ return QPixmap();
+
+ UIView *view = window ? reinterpret_cast<UIView *>(window) : m_uiWindow;
+
+ if (width < 0)
+ width = qMax(view.bounds.size.width - x, CGFloat(0));
+ if (height < 0)
+ height = qMax(view.bounds.size.height - y, CGFloat(0));
+
+ CGRect captureRect = [m_uiWindow convertRect:CGRectMake(x, y, width, height) fromView:view];
+ captureRect = CGRectIntersection(captureRect, m_uiWindow.bounds);
+
+ UIGraphicsBeginImageContextWithOptions(captureRect.size, NO, 0.0);
+ CGContextRef context = UIGraphicsGetCurrentContext();
+ CGContextTranslateCTM(context, -captureRect.origin.x, -captureRect.origin.y);
+
+ // Draws the complete view hierarchy of m_uiWindow into the given rect, which
+ // needs to be the same aspect ratio as the m_uiWindow'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.
+ [m_uiWindow drawViewHierarchyInRect:m_uiWindow.bounds afterScreenUpdates:NO];
+
+ UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
+ UIGraphicsEndImageContext();
+
+ return QPixmap::fromImage(qt_mac_toQImage(screenshot.CGImage));
+}
+
UIScreen *QIOSScreen::uiScreen() const
{
return m_uiScreen;