Archive:Development/Tutorials/KPixmapCache (zh CN): Difference between revisions

    From KDE TechBase
    (New page: {{Template:I18n/Language Navigation Bar|Development/Tutorials/KPixmapCache}} == 介绍 == {{class|KPixmapCache}} provides disk-caching of QPixmaps. If you're using SVGs or generating pixma...)
     
    No edit summary
    Line 1: Line 1:
    {{Template:I18n/Language Navigation Bar|Development/Tutorials/KPixmapCache}}
    {{Template:I18n/Language Navigation Bar|Development/Tutorials/KPixmapCache}}
    == 介绍 ==
    == 介绍 ==
    {{class|KPixmapCache}} provides disk-caching of QPixmaps. If you're using SVGs or generating pixmaps from some data, you might consider using it as it eliminates the need to generate the same pixmaps over and over again.
    {{class|KPixmapCache}} 对QPixmaps提供磁盘缓存。如果你使用矢量图形或从数据产生位图,那么缓存机制可以避免一次又一次从数据中产生同样的位图。


    == 使用 ==
    == 使用 ==
    === 创建缓存对象 ===
    === 创建缓存对象 ===
    It's API is similar to that of the QPixmapCache.
    API和QPixmapCache的差不多。
    To use it, you first need to create the cache object, using a unique name:
    使用前,先创建一个缓存对象,并起个不重复的名字:
    <code cppqt>
    <code cppqt>
    KPixmapCache* cache = new KPixmapCache("myapp-images");
    KPixmapCache* cache = new KPixmapCache("myapp-images");
    Line 12: Line 12:


    === 插入并发现位图 ===
    === 插入并发现位图 ===
    Once that is done you can easily retrieve and insert images with insert() and find() methods:
    然后用insert()或find()方法就可以很容易的获取插入图像了:
    <code cppqt>
    <code cppqt>
    QPixmap pix("file1.png");
    QPixmap pix("file1.png");
    Line 19: Line 19:
    QPixmap pix2;
    QPixmap pix2;
    if (cache->find("file2.png", pix2)) {
    if (cache->find("file2.png", pix2)) {
         // The pixmap was loaded from cache.
         // 从缓存中加载图像
    } else {
    } else {
         // Pixmap was not found in the cache.
         // 图像没有在缓存里找到
         // Load and insert it:
         // 加载并插入:
         pix2 = QPixmap("file2.png");
         pix2 = QPixmap("file2.png");
         cache->insert("file2.png", pix2);
         cache->insert("file2.png", pix2);
    Line 29: Line 29:


    === 简单方法 ===
    === 简单方法 ===
    As loading pixmaps from files with the aid of cache is a very common operation, KPixmapCache provides a convenience method for that:
    从文件加载位图并使用缓存是个常用操作,KPixmapCache提供了简洁的方法:
    <code cppqt>
    <code cppqt>
    QString filename("myfile.png");
    QString filename("myfile.png");
    QPixmap pix = cache->loadFromFile(filename);
    QPixmap pix = cache->loadFromFile(filename);
    </code>
    </code>
    This first checks if the given file is in the cache. If it is, it's loaded from the cache, otherwise it's loaded from the file and inserted into cache.
    首先检查给定文件是否已经在缓存里了。如果在,从缓存加载;否则从文件加载并加到缓存里面。


    Similar method is provided for loading SVG files:
    对矢量文件,有相同的方法:
    <code cppqt>
    <code cppqt>
    QString filename("myfile.svg");
    QString filename("myfile.svg");
    QPixmap pix = cache->loadFromSvg(filename);
    QPixmap pix = cache->loadFromSvg(filename);
    </code>
    </code>
    This renders contents of the SVG file onto pixmap, using SVG's default size by default. If you need a different size, you can specify it as the second argument of that method:
    这会把矢量文件转化为位图,矢量图的缺省大小就是转化后的大小。如果你要指定一个其它的大小,只要在第二个参数里指定就可以了:
    <code cppqt>
    <code cppqt>
    QString filename("myfile.svg");
    QString filename("myfile.svg");
    Line 47: Line 47:
    QPixmap pix = cache->loadFromSvg(filename, size);
    QPixmap pix = cache->loadFromSvg(filename, size);
    </code>
    </code>
    This renders the SVG onto a pixmap with the size of 150x250 pixels.
    这里矢量数据会被转化成位图,大小为150x250象素。




    === 时间戳 ===
    === 时间戳 ===
    Often you need to make sure the cache is up-to-date and you're not using obsolete pixmaps. For this, the cache provides timestamp() method. By default it returns time when the cache was created, but you can set your own timestamp with setTimestamp() method.
    通常,缓存里的内容要确保是最新的。过时的内容就要被更新了。为此,缓存提供了一个timestamp() 时间戳方法。缺省返回缓存创建时间,但你可以通过setTimestamp()方法设置你认为的时间。


    <code cppqt>
    <code cppqt>
    // Our data
    // 数据
    MyDataObject data;
    MyDataObject data;
    // Make sure the cache is up to date
    // 确保缓存足够新
    if (cache->timestamp() < data.timestamp()) {
    if (cache->timestamp() < data.timestamp()) {
         // Data is newer than the cache
         // 数据比缓存的要新
         // Thus the cache is obsolete, delete and reinitialize it
         // 缓存过时了,撤销,重载
         cache->discard();
         cache->discard();
    }
    }
    // Here the cache is always recent enough
    // 缓存内容足够新
    QPixmap visualizedData;
    QPixmap visualizedData;
    QString key("data-visualization");
    QString key("data-visualization");
    // Try to find the visualization from the cache
    // 试图从缓存里找图像
    if (!cache->find(key, visualizedData)) {
    if (!cache->find(key, visualizedData)) {
         // Visualization is not in the cache, recreate it...
         // 图像不在缓存,重新创建...
         visualizedData = createVisualization(data);
         visualizedData = createVisualization(data);
         // ...and put the resulting image into cache
         // ...把结果放入缓存里
         cache->insert(key, visualizedData);
         cache->insert(key, visualizedData);
    }
    }
    // Show the visualizedData pixmap to user
    // 给用户展现那副图像。
    </code>
    </code>
    This example creates a visual representation of some data. If the data is more recent than the cache, then contents of the cache are deleted using the discard() method. Next, we check if cache already contains the necessary pixmap. If it does, then there is no need to recreate it from the data (which might be expensive).
    这个例子展示了如何显示一些数据。如果数据比缓存里的新,那就用discard()方法删除缓存内容。下一步, 检查缓存是否已经有了需要的位图。如果是,就不必在从数据产生了(这事很耗资源的操作)。


    === 应用程序接口(API)文档 ===
    === 应用程序接口(API)文档 ===
    For a list of all KPixmapCache methods, check out its [http://api.kde.org/classmapper.php?class=KPixmapCache&module=kdelibs&version=4.0 API docs].
    要看所有的KPixmapCache方法, 这里有所有的[http://api.kde.org/classmapper.php?class=KPixmapCache&module=kdelibs&version=4.0 API 文档].

    Revision as of 10:20, 5 April 2009


    Development/Tutorials/KPixmapCache

    介绍

    KPixmapCache 对QPixmaps提供磁盘缓存。如果你使用矢量图形或从数据产生位图,那么缓存机制可以避免一次又一次从数据中产生同样的位图。

    使用

    创建缓存对象

    API和QPixmapCache的差不多。 使用前,先创建一个缓存对象,并起个不重复的名字: KPixmapCache* cache = new KPixmapCache("myapp-images");

    插入并发现位图

    然后用insert()或find()方法就可以很容易的获取插入图像了: QPixmap pix("file1.png"); cache->insert("file1.png", pix);

    QPixmap pix2; if (cache->find("file2.png", pix2)) {

       // 从缓存中加载图像
    

    } else {

       // 图像没有在缓存里找到
       // 加载并插入:
       pix2 = QPixmap("file2.png");
       cache->insert("file2.png", pix2);
    

    }

    简单方法

    从文件加载位图并使用缓存是个常用操作,KPixmapCache提供了简洁的方法: QString filename("myfile.png"); QPixmap pix = cache->loadFromFile(filename); 首先检查给定文件是否已经在缓存里了。如果在,从缓存加载;否则从文件加载并加到缓存里面。

    对矢量文件,有相同的方法: QString filename("myfile.svg"); QPixmap pix = cache->loadFromSvg(filename); 这会把矢量文件转化为位图,矢量图的缺省大小就是转化后的大小。如果你要指定一个其它的大小,只要在第二个参数里指定就可以了: QString filename("myfile.svg"); QSize size(150, 250); QPixmap pix = cache->loadFromSvg(filename, size); 这里矢量数据会被转化成位图,大小为150x250象素。


    时间戳

    通常,缓存里的内容要确保是最新的。过时的内容就要被更新了。为此,缓存提供了一个timestamp() 时间戳方法。缺省返回缓存创建时间,但你可以通过setTimestamp()方法设置你认为的时间。

    // 数据 MyDataObject data; // 确保缓存足够新 if (cache->timestamp() < data.timestamp()) {

       // 数据比缓存的要新
       // 缓存过时了,撤销,重载
       cache->discard();
    

    } // 缓存内容足够新 QPixmap visualizedData; QString key("data-visualization"); // 试图从缓存里找图像 if (!cache->find(key, visualizedData)) {

       // 图像不在缓存,重新创建...
       visualizedData = createVisualization(data);
       // ...把结果放入缓存里
       cache->insert(key, visualizedData);
    

    } // 给用户展现那副图像。 这个例子展示了如何显示一些数据。如果数据比缓存里的新,那就用discard()方法删除缓存内容。下一步, 检查缓存是否已经有了需要的位图。如果是,就不必在从数据产生了(这事很耗资源的操作)。

    应用程序接口(API)文档

    要看所有的KPixmapCache方法, 这里有所有的API 文档.