The Problem
My app is intended to display dynamic images managed by a server. Including the entire library of images as AppResources is unfeasible and would bloat the size of the application to astronomical proportions (not to mention the time involved in translating all the images into 6 different draw sizes each). Pointing an Image tag at the relevant URL’s results in very buggy operation on Android 5.1.1 on an emulated Google Pixel XL:
- images are repeated across tags as you scroll until the correct image request is fulfilled, then it’s properly displayed
- nothing is displayed at all because the request is never fulfilled.
- the console is going haywire with
System.err: java.net.ProtocolException: unexpected end of stream
over and over again
The Goal
The images belong to a dataset as a property of each item in the set. When I go to retrieve the dataset from the server, I’d like to download the images and store them in cache as well, during the fulfillment of that initial promise to get the dataset. Then, after all the downloading is done, I want to display the images from cache, along with the rest of the data from the set.
I also want this to happen on the latest version of nativescript possible, and be functional on iOS as well…
The History
I have attempted to use several different approaches to manually manage the image cache:
tns plugin add nativescript-image-cache
tns plugin add nativescript-web-image-cache
import * as imageCache from 'ui/image-cache'
import * as imageSource from 'image-source'
Following what documentation is available on each of those packages, I can’t seem to make it work using nativescript v3.4.2 and tns-core-modules v3.4.1.
I attempted using imageSource’s fromUrl()
getter at one point, but that also resulted in many unexpected end of stream
errors…its as if while I’m looping through the set, I’m starting too many requests for the phone to deal with it. That’s why, ideally, I’m loading things into cache based on a URL key, and pulling them out based on the URL key, so each image will only be requested once. The real kick-in-the-gut is making this synchronous. I need to pause, and hold the user at a “Loading…” prompt until all the necessary images are fully loaded…
And I did find this documentation which explains to use the getImage()
function on the http service provider instead…however, I can’t seem to find the correct definition of the http service provider where the function actually exists - it doesn’t exist on import { Http } from '@angular/http';
…