3

I would like to add an image to my C++ project in Xcode so that I can read that image and do something with it. How do I include the image into my project?

enter image description here

tried copy pasting both into my project and into the folder with my c++ source.

Community
  • 1
  • 1
Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • 1
    can't you just copy-paste it into your project folder...? – Rana Jan 29 '16 at 20:45
  • I'm not sure why the project would care about image files. What is it you want to do that it won't let you? – Galik Jan 29 '16 at 20:47
  • Is this for an application? Usually images are resources in the application bundle. If you just drag the file into the Xcode project it should do that for you. As to how you actually load the graphic at runtime, that's a different question. – Dan Korn Jan 29 '16 at 20:50
  • Yes I tried copy pasting the images into my project folder but I am getting an error when trying to read the image. – Lightsout Jan 29 '16 at 20:59
  • Possible duplicate: [Adding resource files to Xcode](http://stackoverflow.com/q/10247680/3425536) – Emil Laine Jan 29 '16 at 21:27

2 Answers2

2

In Xcode 10, I can't put the image in the same folder as the executable, and I don't think it's a good idea anyway as this folder gets deleted when a clean is performed.

The solution I found when dealing with "Command Line Tool" projects in Xcode, is to modify or create a new Copy File Phase (and not a Copy Bundle Resources one) in Targets > Build Phases. But the default values should be modified:

  1. Destination: Resources
  2. Subpath: Nothing
  3. Uncheck Copy only when installing

The image should then be added to this phase (using the + button). Beforehand, when adding the image to the project, selecting a target is not necessary, adding it to the Copy Files Phase will do it automatically.

xcode

The code will then look like this:

std::string filename("input.bmp");
bitmap_image image(filename);
nyg
  • 2,380
  • 3
  • 25
  • 40
  • Yes this works, thank you. I'm new to c++ development, do I always have to add an image to that "copy files" list when I add an image to a specific folder? e.g. I created a group/folder called assets in my project. – Andrei Calazans May 16 '19 at 22:20
0

Put the image in the same folder with the executable i.e. with the file in the Products folder.

Lightsout
  • 3,454
  • 2
  • 36
  • 65