-1

What is wrong with this method? The problem is that rotated image has transparent box after rotation. How can I get rid of it?

@implementation UIImage (RotationMethods)

static CGFloat getRadianFromDegree(CGFloat degrees)
{return degrees * M_PI / 180;};

- (UIImage *) rotateImageByDegree:(CGFloat)degrees
{
    UIView *rotatedImageView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(getRadianFromDegree(degrees));
    rotatedImageView.transform = t;
    CGSize rotatedSize = rotatedImageView.frame.size;
    UIGraphicsBeginImageContextWithOptions(rotatedSize, YES, self.scale);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
    CGContextRotateCTM(bitmap, getRadianFromDegree(degrees));
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
    UIImage *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return rotatedImage;

}

@end

EDIT1
enter image description here

EDIT2 Here is the code of rotate action:

- (IBAction)rotateAction:(id)sender {
    self.imageView.image = [self.imageView.image rotateImageByDegree:22];
}
astrolka
  • 133
  • 1
  • 11
  • I'd guess that the issue lies in the contentMode of the imageView - the rotated image do not fit the imageView anymore so it is downscaled. – Mr. Hedgehog Apr 27 '17 at 17:29
  • @matt `UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, self.scale);` I have changed and with no result also edited the question and added an image which illustrate the problem – astrolka Apr 27 '17 at 17:55
  • @Mr.Hedgehog I've just edited the question – astrolka Apr 27 '17 at 17:55
  • @matt I have changed it to YES and uploaded another image hope it will show my problem – astrolka Apr 27 '17 at 18:27
  • Make sure the `backgroundColor` of the `UIImageView` is set to `[UIColor clearColor]`. – Sulthan Apr 27 '17 at 18:46
  • @Sulthan the problem is in that that image is getting smaller after the second rotation – astrolka Apr 27 '17 at 18:48
  • Could you specify exactly what result you _want_ to get? How big should the resulting `image` be, precisely, and how should the drawn picture of the kittens lie within it? – matt Apr 27 '17 at 19:05
  • @matt I got this code from this answer http://stackoverflow.com/a/11667808 – astrolka Apr 27 '17 at 19:06
  • @matt wow to rotate the original image is really good idea! but to rotate only image view I cannot as the user will share this image. Thank you! – astrolka Apr 27 '17 at 19:08

1 Answers1

1

The problem is caused by code you are not showing us.

How do I know? I ran your code, the code you did show — and there is no gap between the rotated image and the image boundary:

enter image description here

In that screen shot, the main view background is red, and I have deliberately set the image view background to white so that you can see that the corners of the rotated kittens picture meet the containing image edges.

If we set the image view background to clear, the white disappears and we see the rotated kittens on the red background.

enter image description here

Thus we may conclude that the "shrinkage" you are complaining about is something you are doing, something that you have not revealed in your question.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • the problem is this: `- (IBAction)rotateAction:(id)sender { self.imageView.image = [self.imageView.image rotateImageByDegree:22]; }` all I needed to do was to store actual angle and rotate initial image: `- (IBAction)rotateAction:(id)sender { self.angle += 22; self.imageView.image = [[UIImage imageNamed:@"kittens"] rotateImageByDegree:self.angle]; }` Please update the answer I'll accept it – astrolka Apr 27 '17 at 20:13