Reputation: 699
I can't draw my rotated image on top of another image. I tried several ways to do that, but with no success. My backgroundImg is alright, but my logoImageView is not rotated. Why? Here is my code:
CGSize newSize = CGSizeMake(555, 685);
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[backgroundImg.image drawInRect:CGRectMake(0, 0, 555, 685)];
CGAffineTransform rotate;
rotate = CGAffineTransformMakeRotation((rotationSlider.value + 360) * M_PI / 180.0);
logoImageView.layer.anchorPoint = CGPointMake (0.5, 0.5);
logoImageView.transform = CGAffineTransformMakeScale (1, -1);
[logoImageView setTransform:rotate];
Then I try 1):
[logoImageView.image drawAtPoint:CGPointMake(logoImageView.center.x, logoImageView.center.y)];
And 2):
[logoImageView.image drawInRect:CGRectMake(0, 0, logoImageView.bounds.size.width * 2.20, logoImageView.bounds.size.height * 2.20) blendMode:kCGBlendModeNormal alpha:1];
Finishing the drawing like this:
imageTwo = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Neither works - my logoImageView is not rotated. What's the problem? I want my logoImageView.image to be rotated in the combined image.
Upvotes: 4
Views: 171
Reputation: 7685
What you're doing here is setting the transform
-property of logoImageView
. This property specifies the transform applied to the UIImageView
itself. While this will make the image appear rotated when the image view is displayed, it doesn't change the underlying image.
So when you rotate the image view and read the image
-property of your image view, you still get the exact same image as you assigned to it since the transform is applied to the view and not to the image itself.
What you want to do is draw the image to the CGContext
with a rotated transform. To set this transform you have to use the the CGContextRotateCTM
function. This function sets the 'Current Transformation Matrix' which specifies the transform to be applied when drawing in the context. I also use CGContextTranslateCTM
to move the image to the center of the context.
The final code may look like this:
CGSize newSize = [flowersImage size];
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[flowersImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), newSize.width / 2.f, newSize.height / 2.f);
CGContextRotateCTM(UIGraphicsGetCurrentContext(), -M_PI/6.f);
[appleImage drawAtPoint:CGPointMake(0.f - [appleImage size].width / 2.f, 0.f - [appleImage size].height / 2.f)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Upvotes: 3