Cropping vs scaling and memory use

Hi,


I've been working on my cropping and scaling flow and have noticed that there are big memory spikes when resizing an image (both using ImageIO and drawing in a context). What I don't get is that I see no memory spikes when cropping an image. My mental model says that cropping should decompress the image into memory and then copy the pixels to a new image object. Is that wrong?

Replies

It depends on exactly what you're doing.


In general, a crop operation could just retain the original backing store (sharing it with the original image) and keep metadata so it "knows" that it's just a portion of it. When it draws or whatever, it would just draw that portion.

As Ken notes, depends...


Maybe some discussion here:

https://developer.apple.com/videos/play/wwdc2018/416/


Also, don't forget you can use Instruments to dig in rather than rely strictly on your mental model...it might be fun to validate that model, too 😉


Ken