Unable to draw rotated image

I cannot draw a rotated image on top of another image. I tried several ways to do this, but to no avail. My backgroundImg is fine, but my ImageView logo does not rotate. What for? 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 will 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]; 

Completion of the drawing as follows:

 imageTwo = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

Nothing works - my ImageView logo doesn't rotate. What is the problem? I want my ImageView.image logo to rotate in a composite image.

+4
source share
1 answer

What you are doing here is the transform -property logoImageView . This property indicates the transformation applied to the UIImageView itself. Although this will cause the image to be rotated when the image is displayed, it will not change the base image.
Therefore, when you rotate the representation of the image and read the image -property of your image, you still get the same image that you assigned to it, since the transformation applies to the representation, and not to the image itself.

What you want to do is draw an image on a CGContext using a rotated transform. To establish this conversion, you must use the CGContextRotateCTM function. This function sets the "Current Transformation Matrix", which defines the transformation that should be applied when drawing in context. I also use CGContextTranslateCTM to move the image to the center of the context.

The final code might 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(); 
+3
source

Source: https://habr.com/ru/post/1501851/


All Articles