UIImage rotation problem

I am developing an iOS 6 application for the iPad. I developed code that rotates UIImage . It works great with square images, but when the images are not square, they are cropped, so you only see part of the image (square).

My code is:

 UIGraphicsBeginImageContext(image.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM( context, 0.5f * image.size.width, 0.5f * image.size.height ) ; CGContextRotateCTM( context, -1.5707963267949) ; [image drawInRect:(CGRect){ { -imatgetemporal.size.width * 0.5f, -image.size.height * 0.5f }, image.size }]; UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 

I think the problem is in 0.5f, but I don’t know how to solve it. What can I do?

+4
source share
1 answer

You need to make your graphics context large enough for the rotated image. You use image.size, but you must use the rotated size as an argument for the UIGraphicsBeginImageContext. Also, your drawInRect argument is incorrect. You need to compensate for the (rotated) beginning of x, which defines the y source image of the final image as follows: β€œUp” to half the original height (which completely excludes the final image from the top of the drawing context), then β€œdown” to the full original width, which is final image height.

 const CGSize imageSize = image.size; UIGraphicsBeginImageContext(CGSizeMake(imageSize.height, imageSize.width)); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0.5f * imageSize.width, 0.5f * imageSize.height ) ; CGContextRotateCTM(context, -1.5707963267949) ; [image drawInRect:(CGRect){ { imageSize.height * 0.5 - imageSize.width, -imageSize.width * 0.5f }, imageSize }]; UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
+1
source

All Articles