I have UIImageView
in which I'm showing 50x100 image.
I want to show only a part of image 50x50 (top part)?
How can I do that?
I have UIImageView
in which I'm showing 50x100 image.
I want to show only a part of image 50x50 (top part)?
How can I do that?
I might have a solution for you. See if this works. In Interface Builder there is an option about Image content fill properties. You can set it to
top-left
. Follow the image in interface builder -After this set the size of
UIImageView
to 50x50 with "clip-subviews" checked...The very simple way to move big image inside UIImageView as follows.
Let we have the image of size (100, 400) representing 4 states of some picture one below another. We want to show the 2nd picture having offsetY = 100 in square UIImageView of size (100, 100). The solution is:
Here contentFrame is normalized frame relative to real UIImage size. So, "0" means that we start visible part of image from left border, "0.25" means that we have vertical offset 100, "1" means that we want to show full width of the image, and finally, "0.25" means that we want to show only 1/4 part of image in height.
Thus, in local image coordinates we show the following frame
You can crop the image by using
CGImageCreateWithImageInRect
, which is Quartz primitive working on CGImageRef, so you would have something like:When calculation
cropRect
, keep in mind that it should be given in pixels, not in points, i.e.:where the
0.5
factor accounts for the fact that you want the top half only.If you do not want to go low-level, you could add your image to a
UIView
as a background color and use theclipToBounds
CALayer property to make the clipping:also, set
myView
bounds accordingly.