resizes gallery based on photo displayed

This fixes an iOS bug that keeps the gallery at the same height as the tallest image, regardless of whether it is being shown. There is also some light refactoring.