I have an Image inside UIScrollView which i can zoom in and out.
I have a button that let the user rotate the Image 90 degrees:
(void)RotateImage {
CGAffineTransform rotateTrans = CGAffineTransformMakeRotation(-90.0 / 180.0 * 3.14);
BaseImg.transform = rotateTrans;
}
After the imaged is rotated i cannot zoom in and out.. the image is going crazy on the screen and going back to the UNRotated state.
What am i doing wrong? code examples will be great!
Thanks 🙂
UIScrollView likes to take over the transforms of the views it contains. There are two solutions:
To rotate the image, see How to Rotate a UIImage 90 degrees?. If you’re always and only doing 90 degree rotation, see @Peter Sarnowski’s solution. To adapt it to what you’re doing here, assuming that
BaseImgis aUIImageView:This will only rotate once. To have rotateImage work repeatedly, read the existing
orientationproperty and move it on to the next in clockwise or anticlockwise order.If the image is not square, you may also need to resize
baseImgto reflect its new aspect ratio.To create a UIView subclass, you need to have it store a
CALayeras a sublayer of the view layer. Store the image in the sublayer, and transform the sublayer at will. This is faster, and allows arbitrary rotation, but you need to calculate your own scaling to prevent the rotate image going outside the view bounds.