iosuiimageviewcontentmode

Show a Specific Part of an Image


Let's say I have an UIImageView which is significantly smaller than the image it's showing. I set contentMode to UIViewContentModeScaleAspectFill to make it look like you're seeing only a specific part of the image. It automatically shows the center of the image.

I don't want to use the other contentModes like top, bottom, etc because in my app you can drag the imageView and as you move it, it shows the parts of the image that are under the imageView. In other words the imageView is like a window to the image itself.

How can I change which part of the image the imageView is showing?

Thanks!


Solution

  • Various possibilities here:

    The second one is easier because the scroll view responds to dragging, whereas with the first possibility you'd have to detect dragging separately and keep setting the contentsRect in response, thus reinventing the wheel.