Path to image in xcassets folder (iOS)

  • Hi!

    Hope you can help me with this... I have a large number of images that i want to use in an iOS app. Adding them to a .qrc does not work ("xcode build failed"), so i made a .xcassets file in xcode to which I want to add images. However, no images are shown when running on an iPhone (but it does work in the simulator). I followed the advice here.

    How do i find the correct path to my images?

    Thanks in advance!

    My .pro file:

    QT       += core gui multimediawidgets
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    TARGET = AssetsTest
    TEMPLATE = app
    SOURCES += main.cpp\
    HEADERS  += mainwindow.h
    FORMS    += mainwindow.ui
    ios {
        assets_catalogs.files = $$files($$PWD/*.xcassets)
        QMAKE_BUNDLE_DATA += assets_catalogs

    And my .cpp file:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        QPixmap pixmap("//Users/JCROHNER/Programmering/QT Projekt/AssetsTest/Media.xcassets/Photo 2014-05-10 11 33 08.imageset/Photo 2014-05-10 11 33 08.jpg");

  • Lifetime Qt Champion


    You hardcoded the path to your image on your computer so it won't be valid at all on your device. You should rather use QCoreApplication::applicationDirPath and move to the right folder from there.

  • Tanks a lot! Your suggestion solved my problem.

  • Here is the code that works:

        QString path = QCoreApplication::applicationDirPath() + "/Photo 2014-05-10 11 33 08.jpg";
        QPixmap pixmap(path);

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.