Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

UIImageView Rotation Issue

Right now I'm blending two images and one of the UIImages has the rotation gesture. but when I save the images the one with the rotation gesture doesn't save correctly it just stays the same and not rotated to the way the user placed it.

Here is my code.

- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {



    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    recognizer.rotation = 0;


}
- (UIImage *) blendImages {
    UIImage *photoImage = self.photoView.image ;
    UIImage *toeImage = self.stacheView.image;


    CGSize photoSize = CGSizeMake(photoImage.size.width, photoImage.size.height);


    UIGraphicsBeginImageContext( photoSize );


    [photoImage drawInRect:CGRectMake(0,0,photoSize.width,photoSize.height)];

    CGPoint origin = self.stacheView.frame.origin;
    CGSize size = self.stacheView.frame.size;

    CGFloat xScale = photoImage.size.width / self.view.bounds.size.width;
    CGFloat yScale = photoImage.size.height / (self.view.bounds.size.height-44);


    [toeImage drawInRect:CGRectMake((origin.x * xScale), (origin.y * yScale),
                                       size.width * xScale, size.height * yScale)
                  blendMode:kCGBlendModeNormal alpha:1.0];


    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}

2 Answers

That's because your affine transform was applied to the view, which only serves as a container of the image you drew to your context. When you save it out, the image is read from your UIImageView (a container) without the transformation since the transform was applied to the view and not the image.

To save the rotation as well, you need to draw with the rotation. Check out the CGContextRotateCTM function.

https://developer.apple.com/library/ios/documentation/graphicsimaging/Reference/CGContext/Reference/reference.html#//apple_ref/c/func/CGContextRotateCTM

Am I implementing the CGContextRotateCTM function in the rotation gesture or my image blending code? because I've been trying to put it in my blending code and it keeps rotating my photoView and distorting it.