0

I'm building an app using Xamarin.Forms, and i have a "gallery page". This page consists of many images that are being loaded into the page.

The loading of the images takes quite a while, and when navigating to another page, and afterwards coming back using Navigation.PopAsync and Navigation.PushAsync the entire page needs to be rebuild again (photo's are being loaded all over again).

I've tried caching the page (so not doing Navigation.PushAsync(new GalleryPage()) all the time, but keeping it in a variable, but this makes no difference.

Does anyone know how to cache an entire page, so it does not need to be loaded all over again?

Thank you

Maarten
  • 1
  • 1
  • 1

2 Answers2

0

If you keep the Page Object in a variable so you are doing

Page myPage = new Page();
Navigation.PushAsync(myPage);

then later do

Navigation.PushAsync(myPage);

that should certainly keep the page loaded in memory.

Images are generally also cached. So if you are getting them from a URL then they will be cached.

If you are getting them from a database this might be the source of your delay, especially if you ask it to go and refresh the list.

Adam
  • 16,089
  • 6
  • 66
  • 109
  • I've tried that, the images are loaded from the filesystem. However, when the page is pushed back on the stack, there is a long delay until the page is shown. (It draws the page completely before showing, getting many messages like "[Choreographer] Skipped 486 frames! The application may be doing too much work on its main thread."). There is no code in the "OnAppearing" method. – Maarten May 19 '15 at 11:27
  • I would need to see your code to give you any more direction. You seem to be overloading the UI thread and it may also be an emulator issue. How does your code run on a real Android device? – Adam May 19 '15 at 13:17
  • If you want to show the View code we would be able to give you more direction to see if you have Grid, ListView, StackLayout etc. – Adam May 20 '15 at 09:08
0

The problem likely lies with the size and number of images you're showing.

I've dealt with the same issue by in two steps:

  • Create thumbnails - large images take a lot longer to load and if you have a lot of them on the screen they are all small. It may be well worth scaling them down as they come in and storing the thumbnail. A camera-sourced image can be 5-8Mb when a thumbnail may be 20-30K. The large image will also have to be scaled down every time you show it.

  • Use ListView to show the images rather than any Grid, StackLayout or whatever else you may have. The difference is that ListView is rendered more dynamically, it creates rows only as they are about to be shown, so not all of the images are loaded in memory. Alternatively you can create your own logic that loads and unloads images dynamically. Worst case to watch out for is using a scroll view filled with images - they will likely attempt to render even when they are off-screen

Community
  • 1
  • 1
Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
  • Thanks for your reaction, we are already using Thumbnails but are not using a Listview. I will have a try and will come back asap. – Maarten May 20 '15 at 07:56
  • I've just checked some things about Listviews, and i've got a question. We are trying to create a gallery like this example (http://sapegin.github.io/jquery.mosaicflow/). Just images in different sizes. Is this possible with a listview, because there are no columns? – Maarten May 20 '15 at 08:36
  • I don't think you can do this in a list view in a reasonable way. You can implement a native solution and create a custom render for it (better performance, more code) or you can build one out of standard XF views (lower performance, simpler to do) – Sten Petrov May 20 '15 at 14:15
  • The whole point though remains the same: to only have UI elements that correspond to images that are at least partially visible or about to be, and dismiss the ones aren't visible – Sten Petrov May 20 '15 at 14:22
  • Shouldn't scrollview do that for you? – Maarten May 21 '15 at 12:16
  • No, scrollview doesn't virtualize – Sten Petrov May 21 '15 at 14:50