简体   繁体   English

如何反复使用QImage?

[英]How to use QImage repeatedly?

I have a horizontal sprite of images. 我有一个水平的图像精灵。 This one for example: 以这个为例:

在此处输入图片说明

All the images have the same width and height. 所有图像具有相同的宽度和高度。 I have stored it in my resources file, and declared it in my application by a define : 我已经将其存储在资源文件中,并通过define在应用程序中声明了它:

#define SPRITE QImage(":/sprite.png")

So there are 4 images that I will need several times in my application. 因此,我的应用程序中将需要4张图像。 In order to do this, I implemented this function which retrieves the image at the position n in the sprite. 为了做到这一点,我实现了此功能,该功能可检索精灵中位置n处的图像。

QImage getNthImageFromSprite(int n, QImage sprite)
{
    int size = sprite.height();
    return sprite.copy((n - 1) * size, 0, size, size);
}

Then, I declared a general enum to put names on the position: 然后,我声明了一个通用枚举以将名称放在该位置:

enum eImage
{
    eImage_black = 1,
    eImage_red,
    eImage_orange,
    eImage_green
}

And finally, the function that I'm using anywhere in my application: 最后,我在应用程序中任何地方使用的功能:

QImage getSpriteImage(eImage img)
{
    return getNthImageFromSprite(img, SPRITE);
}

This works well . 这很好用

But I have the impression that this is not very good, since I call the constructor of QImage each time I want to get a specific image. 但是我有一个不好的印象,因为每次我想要获取特定的图像时都会调用QImage的构造函数。 Knowing that a sprite can contain +40 images and that I will need these images several times, should I cache an image the first time it get called, or the way I'm doing it is good? 知道一个精灵可以包含+40张图像,并且我会多次需要这些图像,我是否应该在第一次调用图像时对其进行缓存,或者这样做的方式很好?


Note: I need QImage for various reasons, but a comparison with QPixmap would be appreciated. 注意:出于各种原因,我需要QImage,但希望与QPixmap进行比较。

Replace the define with an object: 用对象替换定义:

static QImage SPRITE(":/sprite.png");

It will be initialized only once on startup. 它只会在启动时初始化一次。

You can even put it in the method: 您甚至可以将其放入方法中:

QImage getSpriteImage(eImage img)
{
    static QImage SPRITE(":/sprite.png");
    return getNthImageFromSprite(img, SPRITE);
}

In this case, it is initialized on first usage. 在这种情况下,它将在首次使用时初始化。

Howerver, you sill create new objects for every call to getNthSpriteImage . 但是,您将为每次对getNthSpriteImage调用创建新对象。 You could use a local static cache to re-use already returned objects: 您可以使用本地静态缓存来重复使用已经返回的对象:

QImage getNthSpriteImage(int n, QImage img) {
    static QMap<int, QImage> cache;
    if (!cache.contains(n)) {
        int size = sprite.height();
        cache[n] = sprite.copy((n - 1) * size, 0, size, size);
    }
    return cache[n];
}

As for the difference for QImage/QPixmap, this is the main difference: 至于QImage / QPixmap的区别,这是主要区别:

QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QImage是为I / O设计的,并针对直接像素访问和操作进行了优化,而QPixmap是为在屏幕上显示图像而设计和优化的。

So I would recommend to use QPixmap if you don't want special image formats (RGBA, premultiplied RGBA, indexed, ...) or use direct pixel manipulation. 因此,如果您不想使用特殊的图像格式(RGBA,预乘的RGBA,索引的...)或使用直接像素操作,则建议使用QPixmap。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM