Turn an arbitrary degree of UIImage

I want to rotate UIImage (not UIImageView) to a custom degree I followed this post , but this did not work for me.

Can anybody help? Thanks.

UPDATE: The code below performs some tasks, but I lose part of the image after rotating it:

enter image description here

What should I change to get it right? (By the way, the yellow color in the screenshots is my UIImageView bg)

- (UIImage *) rotate: (UIImage *) image { double angle = 20; CGSize s = {image.size.width, image.size.height}; UIGraphicsBeginImageContext(s); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(ctx, 0,image.size.height); CGContextScaleCTM(ctx, 1.0, -1.0); CGContextRotateCTM(ctx, 2*M_PI*angle/360); CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width, image.size.height),image.CGImage); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 
+4
source share
5 answers

This method returns the image to the rotation angle.

 #pragma mark - #pragma mark Rotate Image - (UIImage *)scaleAndRotateImage:(UIImage *)image { CGImageRef imgRef = image.CGImage; CGFloat width = CGImageGetWidth(imgRef); CGFloat height = CGImageGetHeight(imgRef); CGAffineTransform transform = CGAffineTransformIdentity; CGRect bounds = CGRectMake(0, 0, width, height); CGFloat boundHeight; boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeScale(-1.0, 1.0); transform = CGAffineTransformRotate(transform, M_PI / 2.0); //use angle/360 *MPI UIGraphicsBeginImageContext(bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextConcatCTM(context, transform); CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef); UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageCopy; } 
+8
source
 - (UIImage *)rotate:(UIImage *)image radians:(float)rads { float newSide = MAX([image size].width, [image size].height); CGSize size = CGSizeMake(newSide, newSide); UIGraphicsBeginImageContext(size); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(ctx, newSide/2, newSide/2); CGContextRotateCTM(ctx, rads); CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(-[image size].width/2,-[image size].height/2,size.width, size.height),image.CGImage); //CGContextTranslateCTM(ctx, [image size].width/2, [image size].height/2); UIImage *i = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return i; } 

this function rotates any image in the center, but the image becomes a square, so I suggest referring to the center of the image when drawing it after this function.

+3
source

You need to solve two things to make this work.

  • You revolve around the bottom corner of the image instead of the center.
  • The bounding box of the resulting image should be larger, now the image is rotated to fit.

To solve the rotation around the center, first transfer to the center, then rotate, then translate back.

 CGAffineTransform transform = CGAffineTransformIdentity; transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2); transform = CGAffineTransformRotate(transform, angle); transform = CGAffineTransformScale(transform, 1.0, -1.0); CGContextConcatCTM(context, transform); // Draw the image into the context CGContextDrawImage(context, CGRectMake(-imageView.image.size.width/2, -imageView.image.size.height/2, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage); // Get an image from the context rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)]; 

To calculate the size of the bounding box, you will need to put a new rotated image into this:

 - (CGRect) getBoundingRectAfterRotation: (CGRect) rectangle byAngle: (CGFloat) angleOfRotation { // Calculate the width and height of the bounding rectangle using basic trig CGFloat newWidth = rectangle.size.width * fabs(cosf(angleOfRotation)) + rectangle.size.height * fabs(sinf(angleOfRotation)); CGFloat newHeight = rectangle.size.height * fabs(cosf(angleOfRotation)) + rectangle.size.width * fabs(sinf(angleOfRotation)); // Calculate the position of the origin CGFloat newX = rectangle.origin.x + ((rectangle.size.width - newWidth) / 2); CGFloat newY = rectangle.origin.y + ((rectangle.size.height - newHeight) / 2); // Return the rectangle return CGRectMake(newX, newY, newWidth, newHeight); } 

You can find these methods in my previous posts and answers here:

Creating a UIImage from a Rotated UIImageView

and here:

Saving 2 UIImages

Hope this helps,

Dave

+2
source

to rotate the image. you can use this IBAction ... for every button click the image will rotate 90 degrees ...

 -(IBAction)rotateImageClick:(id)sender{ UIImage *image2=[[UIImage alloc]init]; image2 = [self imageRotatedByDegrees:self.roateImageView.image deg:(90)]; //Angle by 90 degree self.roateImageView.image = image2; imgData= UIImageJPEGRepresentation(image2,0.9f); 

}

for a rotating image u need to go only UIimage and rotate degrees for the next method

 - (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees 

// --------------------------------------------- --- ------------------------

 #pragma mark - imageRotatedByDegrees Method - (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{ // calculate the size of the rotated view containing box for our drawing space UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)]; CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180); rotatedViewBox.transform = t; CGSize rotatedSize = rotatedViewBox.frame.size; // Create the bitmap context UIGraphicsBeginImageContext(rotatedSize); CGContextRef bitmap = UIGraphicsGetCurrentContext(); // Move the origin to the middle of the image so we will rotate and scale around the center. CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); // // Rotate the image context CGContextRotateCTM(bitmap, (degrees * M_PI / 180)); // Now, draw the rotated/scaled image into the context CGContextScaleCTM(bitmap, 1.0, -1.0); CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

I think this link will help ...! Rotate the original image by clicking a button in Objective C

http://adrianmobileapplication.blogspot.com/2015/03/rotate-original-image-by-clicking.html

0
source

You should do something like this

 YourContainer.transform = CGAffineTransformMakeRotation( 270.0/180*M_PI ); 

I think the rest is what you can understand.

-2
source

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


All Articles