How to resize dynamically loaded image to contain inside a container?

I want to contain a dynamically loaded image inside a container. Setting “resize mode” to “contain” works for static images however, it does not work for dynamically loaded images.