getNextImage() fails to return last frame

getNextImage() returns -1 when reading the last image.