You can get the sensor and deactivate it:
#include <gazebo/sensors/SensorManager.hh>
sensors::SensorPtr sensor = sensors::SensorManager::Instance()->GetSensor(
"world_name::model_name::link_name::sensor_name");
sensor->SetActive(false);
If you are not sure about the name, you can get the names of all sensors and identify yours:
for (sensors::SensorPtr sensor : sensors::SensorManager::Instance()->GetSensors())
gzmsg << sensor->ScopedName() << "\n";
At the same time, you can skip the whole saving images thing, and directly record a video with the camera:
sensors::CameraSensorPtr camera_sensor =
std::static_pointer_cast<sensors::CameraSensor>(sensor);
camera_sensor->Camera()->StartVideo("mp4");
camera_sensor->Camera()->StopVideo();
camera_sensor->Camera()->SaveVideo("~/recording");