What operation costs more - Image loading or image resizing?
-
I have QML Image element.
What operation is more expensive (Performance) - Loading an image that isn't cached (calling the Image Provider) or using an image that is already in the cache but needes to be resize ? -
The answer probably depends on what definition of cost is used.
For most cases, I imagine that loading a new image is more expensive. If the image provider reads a file from a filesystem, there's probably at least two context changes, storage I/O, memory allocation, and image format decoding.
Resizing might not need anything other than allocating memory and the equivalent of decoding. It might still result in context changes and hitting storage if memory swapping is involved.
-
Thanks jeremy_k.
So if I have 2 QML images that use the same source, one is 50x50 size and the other is 100x100 size you recommend me to use one image source and not to create one image source 50x50 and another image source 100x100? -
As jeremy_k already pointed out, loading an image from disk is pretty expensive. Resizing an Image element live using width/height can be considered a noop.
This isn't entirely true of course, because:
- The bigger the area the image fills, the longer it will take to draw it
- The bigger the source image, the longer it will take to draw it
- If the image has alpha channel, it will take longer to draw it
but the act of resizing itself adds nothing.
Now, you talk about scaling down by from 100x100 to 50x50. This offers a different problem, which is that Images with smooth: true, will use linear interpolation and will start to show artifacts when the scale factor is less than 0.5. This might be ok, and it might not. Depends on the usecase.
If it is not ok here is what you can do. If the small image is static and loaded only rarely, use the sourceSize, which for a 50x50 image, though slow, will be acceptable. If you combine it with Image.asynchronous: true, it won't impact the UI / Render threads, so the application won't suffer.
The other alternative if the image needs to be resized live and to sizes only known at runtime, use Image.mipmap.