Fix handling of image cache refcounts. (Bug#20802)