[英]How to convert a CGImageRef to a QPixmap in Qt 6?
In Qt 5, converting a CGImageRef
to a QPixmap
was easy using their QtMacExtras
module and the QtMac::fromCGImageRef
function.在 Qt 5 中,使用他们的
QtMacExtras
模块和QtMac::fromCGImageRef
function 可以轻松地将CGImageRef
转换为QPixmap
。
In Qt 6, QtMacExtras
has been removed.在Qt 6,
QtMacExtras
已经被移除。 Most of the functions there have explicit replacements, except for QtMac::fromCGImageRef
which was removed "due to lack of known clients of the API".那里的大多数函数都有明确的替换,除了
QtMac::fromCGImageRef
被删除“由于缺少已知的 API 客户端”。
Well it turns out I was a client of that API, just not a known one.好吧,事实证明我是那个 API 的客户,只是不为人所知。 N.netheless, now I need to convert a
CGImageRef
to a QPixmap
in Qt 6. N.netheless,现在我需要在 Qt 6 中将
CGImageRef
转换为QPixmap
。
I looked at the source code for QtMac::fromCGImageRef
in Qt 5.15.2, which lead me to a Qt internal function named qt_mac_toQImage
.我查看了 Qt 5.15.2 中
QtMac::fromCGImageRef
的源代码,它引导我找到一个名为qt_mac_toQImage
。 However, it makes use of a ton of internal functions and classes that I don't have access to in Qt 5, much less Qt 6, so that doesn't help.但是,它使用了大量我在 Qt 5 中无法访问的内部函数和类,更不用说 Qt 6 了,所以这无济于事。
How can I go about doing this conversion?我怎样才能go做这个转换呢?
It turns out that a lot of the private Qt stuff used in the Qt 5.15 implementation of qt_mac_toQImage
applied to situations other than attempting to do CoreGraphics rendering to a QImage.事实证明,qt_mac_toQImage 的 Qt 5.15 实现中使用的许多私有
qt_mac_toQImage
内容适用于除了尝试对 QImage 进行 CoreGraphics 渲染之外的情况。 Once I pulled all of those parts out and simplified, the method for converting a CGImageRef to a QImage or QPixmap is fairly straight forward:将所有这些部分取出并简化后,将 CGImageRef 转换为 QImage 或 QPixmap 的方法就相当简单了:
CGBitmapInfo CGBitmapInfoForQImage(const QImage &image)
{
CGBitmapInfo bitmapInfo = kCGImageAlphaNone;
switch (image.format()) {
case QImage::Format_ARGB32:
bitmapInfo = kCGImageAlphaFirst | kCGBitmapByteOrder32Host;
break;
case QImage::Format_RGB32:
bitmapInfo = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host;
break;
case QImage::Format_RGBA8888_Premultiplied:
bitmapInfo = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big;
break;
case QImage::Format_RGBA8888:
bitmapInfo = kCGImageAlphaLast | kCGBitmapByteOrder32Big;
break;
case QImage::Format_RGBX8888:
bitmapInfo = kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big;
break;
case QImage::Format_ARGB32_Premultiplied:
bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
break;
default:
break;
}
return bitmapInfo;
}
QImage CGImageToQImage(CGImageRef cgImage)
{
const size_t width = CGImageGetWidth(cgImage);
const size_t height = CGImageGetHeight(cgImage);
QImage image(width, height, QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
CGContextRef context = CGBitmapContextCreate((void *)image.bits(), image.width(), image.height(), 8,
image.bytesPerLine(), colorSpace, CGBitmapInfoForQImage(image));
// Scale the context so that painting happens in device-independent pixels
const qreal devicePixelRatio = image.devicePixelRatio();
CGContextScaleCTM(context, devicePixelRatio, devicePixelRatio);
CGRect rect = CGRectMake(0, 0, width, height);
CGContextDrawImage(context, rect, cgImage);
CFRelease(colorSpace);
CFRelease(context);
return image;
}
Once you've converted a CGImageRef
to a QImage
, it's simple converting that to a QPixmap
using QPixmap::convertFromImage
.将
CGImageRef
转换为QImage
后,使用QPixmap::convertFromImage
将其转换为QPixmap
很简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.