7
votes

How can I save QML Image into phone memory ??

also if saving the image was applicable , I have this case which I need to add some text to the image (we can imagine it as we have a transparent image[that hold the text] and put it over the second image , so finally we have one image that we can save it into phone memory)

2

2 Answers

7
votes

Not from Image directly. QDeclarativeImage has pixmap, setPixmap and pixmapChange methods, but for some reason there is no property declared. So you cannot use it fom qml. Unfortunately it cannot be used from C++ either - it is a private calsss.

What you can do is paint graphics item to your pixmap and save it to file.

class Capturer : public QObject
{
    Q_OBJECT
public:
    explicit Capturer(QObject *parent = 0);
    Q_INVOKABLE void save(QDeclarativeItem *obj);
};

void Capturer::save(QDeclarativeItem *item)
{
    QPixmap pix(item->width(), item->height());
    QPainter painter(&pix);
    QStyleOptionGraphicsItem option;
    item->paint(&painter, &option, NULL);
    pix.save("/path/to/output.png");
}

Register "capturer" context variable:

int main()
{
    // ...
    Capturer capturer;
    QmlApplicationViewer viewer;
    viewer.rootContext()->setContextProperty("capturer", &capturer);
    // ...
}

And use it in your qml:

Rectangle {
    // ...
    Image {
        id: img
        source: "/path/to/your/source"
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            capturer.save(img)
        }
    }
}
7
votes

With Qt 5.4+ you can do it straight from your Qml with: grabToImage