compress images and cache images in memory

NOTE: 
I've modified some of your code;
And there's still slight memory leak.