Every few weeks I continue to make more progress on the native Swift iPhone application for Quillify. Whilst working on it this weekend, I discovered FastImage. It’s is a Ruby gem that lets you retrieve the width and height of a remote image from a URL. It’s incredibly easy to use. Here’s how I’m doing it for Quillify’s API:
posterDimensions = FastImage.size(movie[:poster])
Storing image dimensions separately from the image itself is a borderline necessity in an iPhone application if the image dimensions are a determining factor in layout. With Quillify’s home feed, I have a
UITableView component with corresponding
UITableViewCell components for each movie. Part of that cell is a
UIImage component for the movie poster images. If you load images in the main thread, it’s UI-blocking, which leaves you in a predicament with two terrible choices; you can load all the images at once (which is bad because you don’t want to load images unless the user actually scrolls down far enough to need them) or you can load them on demand (which results in the UI locking up while the fetching is occurring; which is even worse than the former option). To get around this predicament, you want to load the images asynchronously, after the user is already (potentially) scrolling down the page. That sounds great, except this means that the
heightForRowAt method will execute before the images have arrived, and you need to give concrete numbers. If you give anything other than the correct values, you’ll end up with the UI jumping around as the images arrive and take their proper space.
With FastImage, I’m now grabbing the dimensions of the movie posters from their remote Amazon URLs and storing them directly in the database when the user posts a new movie. A simple Rails migration script allowed me to run through all the preexisting entries in the database and insert their proper dimensions too. Now the JSON payload for the home feed includes these poster dimensions, and the iPhone app’s home feed is butter smooth on initial load. It’s beautiful.
This is just a “weekends when I feel like it” kind of project but I really hope one day I’ll be able to tell you to go in the App Store and download Quillify. It’s an app like no other. Until then, you can always sign up on the website, though I’ll likely remove web-based login if and when the iPhone app premiers.
Meanwhile, if you’re tinkering with an app idea that involves images, plan on storing the dimensions. You’ll likely need them.