Hi,
I’m trying to create an image button for my program but it’s not working for some reason. I’m trying to use this general method to do it
recordButton.setImages(false, true, true,
ImageCache::getFromMemory(BinaryData::record_png,
BinaryData::record_pngSize),
0.5f, Colours::red,
ImageCache::getFromMemory(BinaryData::record,
BinaryData::record_pngSize),
0.5f, Colours::blue,
ImageCache::getFromMemory(BinaryData::record_png,
BinaryData::record_pngSize),
0.5f, Colours::green);
but every time I try to run it it says that it can’t find my image in the Binary Data/it doesn’t exist. I’m adding the image using projucer (using the + arrow and add existing file and trying to save it to the source folder) so I can’t figure out why it wouldn’t see my image. Any ideas? Thanks!
Edit: had different code earlier that didn’t work/was incorrrect. Tried this method which fixed the previous problem but it still does not recognize BinaryData::record_png. I see the Binary Data file created in my files so I’m not sure what the problem is.
From your code, it appears that the dataSize parameter you’re passing to the getFromMemory function is incorrect. In your first image, for instance, you should use BinaryData::normalButton_pngSize as the size parameter instead of BinaryData::normalButton_png.
That is not the case. The image is copied into the button. No need to store the original image.
Also note that copying an image is a cheap operation, because the imageData is shared.
For reference, here is the source code where the image is copied:
It seems you still have typos:
BinaryData::record should probably be BinaryData::record_png as well.
- Can you describe the error, does it compile?
- Did you include BinaryData.h?
- Did you actually add the record.png to the BinaryData via projucer? Maybe you need to resave the project?
You’re right, thanks for the clarification. A ReferenceCountedObjectPtr is used internally to store image data, so the local image scope is not an issue.
Sorry , it is record_png in my code I must have typed it incorrectly in the forum. BinaryData.h is included and when I look in the header it does show record_png as being created so it seems like it did add/save correctly from Projecer (as far as I can tell). But when I try to run the program it says " [build] “BinaryData::record_png”, referenced from:
[build] DeckGUI::DeckGUI(DJAudioPlayer*, juce::AudioFormatManager&, juce::AudioThumbnailCache&) in DeckGUI.cpp.o
[build] ld: symbol(s) not found for architecture arm64
[build] clang: error: linker command failed with exit code 1 (use -v to see invocation)
[build] make[2]: *** [OtoDecks_artefacts/Debug/OtoDecks.app/Contents/MacOS/OtoDecks] Error 1
[build] make[1]: *** [CMakeFiles/OtoDecks.dir/all] Error 2
[build] make: *** [all] Error 2
[proc] The command: /opt/homebrew/bin/cmake --build /Users/name/Desktop/OtoDecks_start_topic_10/build --config Debug --target all -j 10 – exited with code: 2
[driver] Build completed: 00:00:03.015
[build] Build finished with exit code 2"
Thanks!
And this is the JuceHeader that includes Binary Header
/*
IMPORTANT! This file is auto-generated each time you save your
project - if you alter its contents, your changes may be overwritten!
This is the header file that your files should include in order to get all the
JUCE library headers. You should avoid including the JUCE headers directly in
your own source files, because that wouldn't pick up the correct configuration
options for your app.
*/
#pragma once
#include “AppConfig.h”
#include <juce_audio_basics/juce_audio_basics.h>
#include <juce_audio_devices/juce_audio_devices.h>
#include <juce_audio_formats/juce_audio_formats.h>
#include <juce_audio_processors/juce_audio_processors.h>
#include <juce_audio_utils/juce_audio_utils.h>
#include <juce_core/juce_core.h>
#include <juce_cryptography/juce_cryptography.h>
#include <juce_data_structures/juce_data_structures.h>
#include <juce_dsp/juce_dsp.h>
#include <juce_events/juce_events.h>
#include <juce_graphics/juce_graphics.h>
#include <juce_gui_basics/juce_gui_basics.h>
#include <juce_gui_extra/juce_gui_extra.h>
#include <juce_opengl/juce_opengl.h>
#include “BinaryData.h”
#if defined (JUCE_PROJUCER_VERSION) && JUCE_PROJUCER_VERSION < JUCE_VERSION
/** If you’ve hit this error then the version of the Projucer that was used to generate this project is
older than the version of the JUCE modules being included. To fix this error, re-save your project
using the latest version of the Projucer or, if you aren’t using the Projucer to manage your project,
remove the JUCE_PROJUCER_VERSION define.
*/
#error “This project was last saved using an outdated version of the Projucer! Re-save this project with the latest version to fix this error.”
#endif
#if ! DONT_SET_USING_JUCE_NAMESPACE
// If your code uses a lot of JUCE classes, then this will obviously save you
// a lot of typing, but can be disabled by setting DONT_SET_USING_JUCE_NAMESPACE.
using namespace juce;
#endif
#if ! JUCE_DONT_DECLARE_PROJECTINFO
namespace ProjectInfo
{
const char* const projectName = “OtoDecks”;
const char* const companyName = “”;
const char* const versionString = “1.0.0”;
const int versionNumber = 0x10000;
}
#endif
and it’s included like this #include “…/JuceLibraryCode/JuceHeader.h” in my cpp files that is trying to create the Button Image . Also my school set up the header file and the way it is included in the cpp so I have just been trusting that it is correct
So since you are using CMake, did you add the binary data to the target_link_libraries?
juce_add_binary_data (OtoDecks_data
SOURCES
record.png
# ...
)
target_link_libraries (OtoDecks
PRIVATE
OtoDecks_data
# ...
)
Since the error is at link time, it is probably in the target_link_libraries call
Thank you! I think you are right that it must be something with cMake. This is what my cMake file looks like (also provided by school and not really explained haha). And it does look like they commented out the parts that would be needed for BinaryData
"cmake_minimum_required(VERSION 3.22)
project(OTODECKS VERSION 0.0.1)
add_subdirectory(…/JUCE JUCE) # If you’ve put JUCE in a subdirectory called JUCE
juce_add_gui_app(OtoDecks
# VERSION … # Set this if the app version is different to the project version
# ICON_BIG … # ICON_* arguments specify a path to an image file to use as an icon
# ICON_SMALL …
# DOCUMENT_EXTENSIONS … # Specify file extensions that should be associated with this app
# COMPANY_NAME … # Specify the name of the app’s author
PRODUCT_NAME “OtoDecks”) # The name of the final executable, which can differ from the target name
juce_generate_juce_header(OtoDecks)
target_sources(OtoDecks
PRIVATE
Source/Main.cpp
Source/MainComponent.cpp
Source/DeckGUI.cpp
Source/DJAudioPlayer.cpp
Source/WaveformDisplay.cpp
Source/PlaylistComponent.cpp)
target_compile_definitions(OtoDecks
PRIVATE
# JUCE_WEB_BROWSER and JUCE_USE_CURL would be on by default, but you might not need them.
JUCE_WEB_BROWSER=0 # If you remove this, add NEEDS_WEB_BROWSER TRUE to the juce_add_gui_app call
JUCE_USE_CURL=0 # If you remove this, add NEEDS_CURL TRUE to the juce_add_gui_app call
JUCE_APPLICATION_NAME_STRING=“$<TARGET_PROPERTY:OtoDecks,JUCE_PRODUCT_NAME>”
JUCE_APPLICATION_VERSION_STRING=“$<TARGET_PROPERTY:OtoDecks,JUCE_VERSION>”)
// juce_add_binary_data(GuiAppData SOURCES …)
target_link_libraries(OtoDecks
PRIVATE
# GuiAppData # If we’d created a binary data target, we’d link to it here
juce::juce_gui_extra
juce::juce_audio_basics
juce::juce_audio_devices
juce::juce_audio_formats
juce::juce_audio_processors
juce::juce_audio_utils
PUBLIC
juce::juce_recommended_config_flags
juce::juce_recommended_lto_flags
juce::juce_recommended_warning_flags)"
EDIT: I made changes to the cMake file and it does build now! Thank you so much!
By the way, it is fairly easy to format your code in the forum posts.
The way to do this in the post editor is to click on the icon
which looks like this </>
and then paste in your code
That will make your posts easier to read for all. Thanks.
