In a fashion catalogue app's two-column product grid, how should the app work out which image size to request for each photo?
answer
- the cell, not the photo
- layout size times screen density
- known exactly at request time
- round up to the server's next width
- never the original upload
basics
~20 sMultiply the cell's layout size by the screen's density to get its size in pixels, then request the smallest server-made variant at least that wide. Never fetch the original: it wastes cellular data and decodes far larger than the cell.
solid answer
~40 sAn installed app knows the cell's exact size when it builds the request: its layout width times the screen's density factor is the width in **pixels**. A cell 171 layout units wide needs about 513 pixels on a 3x screen and about 342 on a 2x one. The app rounds that up to the next width the server offers (say 640) and asks for that variant. Fetching the original, say a 4000 x 6000 studio shot of several megabytes, spends a cellular user's data on detail nobody can see, and decoding it costs about 96 MB of memory against roughly 2.5 MB for a 640 x 960 variant. Sizing by layout units alone, ignoring density, gives soft photos on dense screens.
code
pseudocode · 6 linescell_px = ceil(cell_layout_width * screen_density)
widths = image.available_widths # ascending, from the API response
chosen = first w in widths where w >= cell_px
if chosen is none:
chosen = last(widths) # largest offered, never the original
url = fill(image.url_template, width = chosen, format = best_decodable_format)go deeper
Remember the formula: layout size times screen density gives the pixels to request, then round up to a width the server offers. Never ask for the original.
Walk through the arithmetic for two devices and show why rounding up to a short list beats exact widths. Explain why the decoded size follows pixels, not file size.
Cover the changes: rotation, split screen, the gallery, a small original. Reuse a larger cached variant instead of fetching, and keep the width list server-owned for old builds.
Frame the request rule as part of one contract every screen follows, so the size list, the cache hit rate and the bytes per session can be tuned centrally without app releases.
## The question behind the question A fashion retailer's catalogue app shows product photos in a two-column grid on phones and a three-column grid on tablets. The photographers upload originals of, say, 4000 x 6000 pixels (an assumption for this walk-through), and the image server can produce smaller **variants** of each one: the same photo resized to a set of widths and re-encoded. The interviewer wants to hear how the app picks which variant to ask for, and why "just download the photo" is the wrong answer. The short version: **request by the view's size in pixels, which the app can compute exactly, and round up to a width the server actually offers.** ## Why the original is the wrong request Asking for the original and letting the image view scale it down looks simple and works on a fast office Wi-Fi network. On the phones that matter it fails twice: - **Bytes on the network.** Half the sessions in this app are on cellular. An original of several megabytes for a cell that shows about half a million pixels sends most of its bytes as detail the screen cannot display. Forty cells in a scroll means tens of megabytes of data plan for one browse. - **Bytes in memory.** A decoded image costs its pixel count, not its file size: about four bytes per pixel. A 4000 x 6000 original decodes to 96,000,000 bytes, about 96 MB, while a 640 x 960 variant decodes to about 2.5 MB. Decoding to the view's size is the image loader's job, and a good loader will downsample, but it still has to download and read the whole file first. - **Time to first paint.** A bigger file takes longer on a weak signal, so the grid stays blank longer. ## Pixels, not layout units Mobile layouts are measured in **layout units** that stay roughly the same physical size across screens; the screen's **density factor** says how many real pixels each unit covers. The app has both numbers at request time, which is the advantage an installed client has over a web page guessing from markup. 1. Measure the cell's laid-out width (and height, if the crop differs from the original's ratio). 2. Multiply by the screen's density factor to get the width in pixels. 3. Look up the widths the server offers for this image (sent in the API response or as a URL template plus an allowed list). 4. Pick the smallest offered width that is **at least** the pixel width; if none is large enough, take the largest offered. 5. Build the URL for that width and the best format this device can decode. Rounding **up** keeps photos sharp; rounding down saves a little and makes every photo slightly soft, which a fashion retailer's product team notices immediately. ## A worked grid Assume the server offers widths of 320, 480, 640, 960, 1280 and 1920 pixels, and the grid has 16-unit gutters (both assumptions for this example). | Device | Screen width (units) | Density | Columns | Cell width (units) | Cell width (pixels) | Requested variant | |---|---|---|---|---|---|---| | Phone, dense screen | 390 | 3x | 2 | 171 | 513 | 640 | | Phone, older screen | 360 | 2x | 2 | 156 | 312 | 320 | | Large phone | 430 | 3x | 2 | 191 | 573 | 640 | | Tablet, portrait | 820 | 2x | 3 | 252 | 504 | 640 | The phone and the tablet land on the same variant even though their screens differ, which is exactly what keeps the number of distinct requests small. Requesting by layout units alone would ask for 171 pixels on the dense phone, a third of what the screen can show. ## When the size changes - **Rotation or a split-screen resize.** The cell width changes, so the app recomputes. If a larger variant of the same photo is already in its cache, it scales that down and skips the request; otherwise it shows the smaller one while the bigger one loads. - **Opening the full-screen gallery.** The gallery is a different view with a different pixel size, so it asks for a different variant; the grid's copy is a ready placeholder. - **A small original.** If the photographer's original is only 800 pixels wide, the server's list stops at what it can honestly produce; the app takes the largest offered width and lets the view scale it up, rather than asking for an upscaled file that adds bytes and no detail. ## Where this answer stops The device's decode and its memory and disk caches belong to the image-loading part of the design; how the CDN caches each variant belongs to the server side; and noticing that the user is in a low-data mode belongs to the network budget, which may then send a lower-density request. The part that stays here is the request itself: **which pixels the app asks for, computed from the view, not from the file.**
- Why not request the exact pixel width each cell needs, such as 513?Every distinct width is a separate object to resize, store and cache, at the CDN and in each app's disk cache. Devices produce dozens of slightly different cell widths, so exact sizes scatter the same photo across many cache keys and most requests miss. Rounding up to a short list of offered widths costs a few extra pixels and keeps requests concentrated on objects that are already cached.
- The user rotates a tablet and the grid cells grow. What does the app request?It recomputes the pixel width. If its cache already holds a variant of that photo at least that wide, for example from the gallery, it shows that one scaled down and requests nothing. Otherwise it keeps the current smaller variant on screen and requests the next offered width, swapping it in when it arrives.
- Where should the list of offered widths come from?From the server, in the API response or as a URL template with an allowed list fetched as configuration. Then new widths ship without an app release, and a build installed two years ago still requests widths the server knows how to serve.
- Download the original photo and let the image view scale it down.
- Size the request in layout units and ignore the screen's density.
- A 300 KB photo file only costs about 300 KB of memory once shown.
- Requesting a full-screen-width image for a grid cell costs nothing extra.
- One size hardcoded in the build suits every phone and tablet.