Issue creating a dynamic image


What MCU/Processor/Board and compiler are you using?

STM32 on custom HW

What LVGL version are you using?


What do you want to achieve?

I have an image object that I’m creating dynamically. In other words, during runtime, data is sent to the MCU running LVGL, which populates the image pixel array ( Every other image object I have is static, i.e., defined at compile time. Those images all work. However, my dynamically created image does not work - nothing is shown on my display (hence all pixels are zeroes).

The only thing I need to do differently from the static image objects is make the pixel array and the lv_img_dsc_t not a ‘const’. This is obvious, as it can’t be a ‘const’. Therefore, I can’t use the LV_IMG_DECLARE macro; instead I declare the variable similarly, just not a const.

Both the pixel array and the lv_img_dsc_t are global variables; they’re not on the stack. I can confirm the pixel array is correct by the time lv_img_set_src is called.

So can LVGL not display an image array that isn’t const? Any ideas where to look in the LGVL code?


Works for me. Here’s a modified version of the lv_example_img_1.c

typedef struct {
    lv_img_header_t header; /**< A header describing the basics of the image*/
    uint32_t data_size;     /**< Size of the image in bytes*/
    uint8_t* data;   /**< Pointer to the data of the image*/
} img_dsc_t;

static img_dsc_t img_cogwheel_2;

void lv_example_img_1(void)
    lv_obj_t * img1 = lv_img_create(lv_scr_act());
    //lv_img_set_src(img1, &img_cogwheel_argb);
    //lv_obj_align(img1, LV_ALIGN_CENTER, 0, -20);
    //lv_obj_set_size(img1, 200, 200);

    // Create a dynamic image
    img_cogwheel_2.header.always_zero = 0;
    img_cogwheel_2.header.w = 100;
    img_cogwheel_2.header.h = 100;
    img_cogwheel_2.data_size = 10000 * LV_IMG_PX_SIZE_ALPHA_BYTE; = LV_IMG_CF_TRUE_COLOR_ALPHA;
    // Allocate on the heap. = malloc(img_cogwheel_argb.data_size);
    // Populate the data array (using the existing one for convenience)
    for (int i = 0; i < img_cogwheel_argb.data_size; ++i)
    {[i] =[i];

    lv_img_set_src(img1, &img_cogwheel_2);
    lv_obj_align(img1, LV_ALIGN_CENTER, 0, -20);
    lv_obj_set_size(img1, 200, 200);

    lv_obj_t * img2 = lv_img_create(lv_scr_act());
    lv_img_set_src(img2, LV_SYMBOL_OK "Accept");
    lv_obj_align_to(img2, img1, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);