From 4373c4436abf6636821489cdd79cffb3e4be5359 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 16 Nov 2023 17:10:50 +0100 Subject: [PATCH] iOS: add helper to convert UIImage to QImage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Going through the UIImage.CGImage flattens the image to monochrome. Change-Id: If74c33badb4e94253b3bf21b6d69bb9f521d6bff Reviewed-by: Tor Arne Vestbø --- src/gui/painting/qcoregraphics.mm | 18 ++++++++++++++++++ src/gui/painting/qcoregraphics_p.h | 15 ++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/gui/painting/qcoregraphics.mm b/src/gui/painting/qcoregraphics.mm index c05ba06146..e8904dff06 100644 --- a/src/gui/painting/qcoregraphics.mm +++ b/src/gui/painting/qcoregraphics.mm @@ -182,6 +182,24 @@ QPixmap qt_mac_toQPixmap(const NSImage *image, const QSizeF &size) #endif // Q_OS_MACOS +#ifdef Q_OS_IOS + +QImage qt_mac_toQImage(const UIImage *image, QSizeF size) +{ + QImage ret(size.width(), size.height(), QImage::Format_ARGB32_Premultiplied); + ret.fill(Qt::transparent); + QMacCGContext ctx(&ret); + if (!ctx) + return QImage(); + UIGraphicsPushContext(ctx); + const CGRect rect = CGRectMake(0, 0, size.width(), size.height()); + [image drawInRect:rect]; + UIGraphicsPopContext(); + return ret; +} + +#endif // Q_OS_IOS + // ---------------------- Colors and Brushes ---------------------- QColor qt_mac_toQColor(CGColorRef color) diff --git a/src/gui/painting/qcoregraphics_p.h b/src/gui/painting/qcoregraphics_p.h index f0640ba500..f2c2ba1db1 100644 --- a/src/gui/painting/qcoregraphics_p.h +++ b/src/gui/painting/qcoregraphics_p.h @@ -23,15 +23,24 @@ #include -#if defined(__OBJC__) && defined(Q_OS_MACOS) -#include -#define HAVE_APPKIT +#if defined(__OBJC__) +# if defined(Q_OS_MACOS) +# include +# define HAVE_APPKIT +# elif defined(Q_OS_IOS) +# include +# define HAVE_UIKIT +# endif #endif QT_BEGIN_NAMESPACE Q_GUI_EXPORT CGBitmapInfo qt_mac_bitmapInfoForImage(const QImage &image); +#ifdef HAVE_UIKIT +Q_GUI_EXPORT QImage qt_mac_toQImage(const UIImage *image, QSizeF size); +#endif + #ifdef HAVE_APPKIT Q_GUI_EXPORT QPixmap qt_mac_toQPixmap(const NSImage *image, const QSizeF &size);