3I have an image that gets manipulated by touch.
Lets say its an image of an arrow that points up. After it's been rotated 180 degrees, so the arrow is pointing down now, I want to reset CGAffineTransform properties so that it thinks now its turned back to 0 degrees.
I want this because I have to translate, rotate, scale etc whether the image's angle is 0 OR 180.
Thanks in advance.
EDITED Ater 5 replies:
Hmm, I am not sure if any of these answers do what I wanted to. I apologize for not being clear.
To prevent above problem and some other ones, I want to reset everything after it has been rotated 180. For example once the image turned 180deg, CGAffineTransform properties knows it's turned 180%. I want to reset the poperties at that time so CGAffineTransform thinks its turned 0 degrees instead of 180, eventhough the image is upside down visually. I want this to happen without any visual changes, soon as its rotated to 180deg.
Hope this is clearer....
If you are trying to reset the transformation, so that the image appears as it did originally, you can simply set the transform back to the identity.
self.imageView.transform = CGAffineTransformIdentity
If you want to apply arbitrary transformations to the transformed image, the easiest thing to do would be to use the CGAffineTransform methods that take in an existing transformation. Just send in the existing transformation. For example:
CGAffineTransform scale = CGAffineTransformMakeScale(zoom, 1); self.imageView.transform = CGAffineTransformConcat(self.imageView.transform, scale);
If you REALLY need the image as it appears without any transformation at all, you'll have to draw it back into another image. That's not that hard either, but I don't recommend it as your first solution. This code works in the context of a UIView category for an arbitrary view, including a UIImageView:
- (UIImage *) capture { CGRect screenRect = self.frame; CGFloat scale = [[UIScreen mainScreen] scale]; UIGraphicsBeginImageContextWithOptions(screenRect.size, YES, scale); CGContextRef ctx = UIGraphicsGetCurrentContext(); [[UIColor blackColor] set]; CGContextFillRect(ctx, screenRect); [self.layer renderInContext: ctx]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
Swift 3, Swift 4 and Swift 5
You can reset a CGAffineTransform with this:
self.imageView.transform = CGAffineTransform.identity
The shorter version is just .identity
like so:
self.imageView.transform = .identity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With