wrapp-archive / WebImage-Android

Asynchronous image loading library for Android
MIT License
41 stars 5 forks source link

Enhancement: Saving the downloaded image first #3

Closed ghost closed 12 years ago

ghost commented 12 years ago

Would it be better if this library would save the downloaded first to the cache-directory? Because now it is being compressed to png after download.

In my opinion, this should be a better way:

  1. Download image to cache-directory without re-encoding/compressing, just save in its original format
  2. When using this image in the memory cache, then convert to bitmap/drawable
nikreiman commented 12 years ago

Hmm, good point. I think that it is generally more efficient to save the converted file, but I can see some cases where keeping the original would be desirable. I would not be opposed to having an option to saving the original images to cache, but I need to ponder a bit as how best to introduce this feature while still supporting the old behavior.

nikreiman commented 12 years ago

Upon further thought, I'm not sure that this is a good idea. The Bitmap.compress() call can save images in lossless format so there is no loss of quality, as WebImage does (ie, PNG format with 100% quality). Although there might be a bit of processing overhead in converting before writing the image to disk, there is also more overhead associated in manually downloading the image bytes by hand to save them.

An advantage of the current architecture is that all image loading and saving is done through higher-level calls in the Android SDK instead of direct byte manipulation. This reduces the complexity of the library, and also lowers the chance of running into dreaded java.lang.OutOfMemory exceptions. Also, converting raw bytes in the memory cache to a usable Drawable object is probably going to be more CPU intensive than just passing a SoftReference to the caller.

Finally, storing the image in memory first allows for the possibility of asynchronously writing to disk in a low-priority asynchronous task. Although WebImage doesn't do that now, it is a feature which I will probably add in the future.

It seems that the main argument here for saving first is performance, both in terms of fetching from the memory cache and in converting formats when saving images. Currently I'm not convinced that changing the existing architecture would provide much of a performance gain in this regard, but if you can write a benchmark to prove me wrong, I'd be happy to reopen this issue. :)