Forum Archive

ui.ImageView contents not cropping to View bounds

iAmMortos

I have a ui.ImageView with content_mode = ui.CONTENT_SCALE_ASPECT_FILL. The documentation implies that using this content mode will crop the parts of the image that fall outside the bounds of the ImageView.

ui.CONTENT_SCALE_ASPECT_FILL
Scale the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.

However, the excess image simply overflows outside the bounds of the View, spilling over other elements, making me sad.

Two questions:
1) Is this a bug? Or is this the expected functionality of ui.CONTENT_SCALE_ASPECT_FILL?
2) If this is indeed the intended behavior, how do you crop a ui.Image simply?

Regarding question 2, I've gotten cropping to work using the PIL.Image.crop(), and converting that cropped PIL.Image into a ui.Image, but this is expensive, and I need these images to be loaded quickly in real-time.

cvp

@iAmMortos be careful, you say ...FIT and you give the doc of ...FILL

But, you are right, this is a new bug, not existing in the past, with ui.CONTENT_SCALE_ASPECT_FILL

iAmMortos

Oops. I goofed writing the post. But in my code, I am indeed using ui.CONTENT_SCALE_ASPECT_FILL. I've updated my original post.

cvp

@iAmMortos Finally, I think it has always been like that. You need to use an intermediate View

iAmMortos

This I can do! Thanks for the tip!

JonB

There is an attribute, maybe only in objc, clipstoBounds, which defines whether a views contents spill out past the edge of it's bounds. I forget if the python ui.Views have clips_to_bounds.

cvp

@JonB never seen in Pythonista UI, not in the doc.

pulbrich

It’s a uiView property, supposed to regulate the behavior of a view’s subviews.

Instance Property
clipsToBounds
A Boolean value that determines whether subviews are confined to the bounds of the view.
mikael

@iAmMortos, Views clip to their bounds by default, but for some reason ImageView does not, which explains some behaviour I did not understand previously, thanks.

As others have said, you can fix this without extra views with:

iv.objc_instance.setClipsToBounds_(True)