Thus, in order to preserve the orientation of the device during the image and pass it to the method below as a parameter. Here, simply give any name to the method and pass the orientation parameter
switch (orientation) { case UIDeviceOrientationPortrait: [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(0.))]; break; case UIDeviceOrientationPortraitUpsideDown: [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(180.))]; break; case UIDeviceOrientationLandscapeLeft: [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(90.))]; break; case UIDeviceOrientationLandscapeRight: [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(-90.))]; break; case UIDeviceOrientationFaceUp: case UIDeviceOrientationFaceDown: default: break;
and the macro that I used here DegreesToRadians() as follows
static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
This will definitely work.
Happy coding :)
EDIT
If the code above does not work, use this
@interface UIImage (RotationMethods) - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees; @end @implementation UIImage (RotationMethods) - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees { // calculate the size of the rotated view containing box for our drawing space UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)]; CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees)); 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, DegreesToRadians(degrees)); // Now, draw the rotated/scaled image into the context 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 *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } @end
and then call the above function below
switch (orientation) { case UIDeviceOrientationPortrait: image = [image imageRotatedByDegrees:0]; break; case UIDeviceOrientationPortraitUpsideDown: image = [image imageRotatedByDegrees:180]; break; case UIDeviceOrientationLandscapeLeft: image = [image imageRotatedByDegrees:-90]; break; case UIDeviceOrientationLandscapeRight: image = [image imageRotatedByDegrees:90]; break; case UIDeviceOrientationFaceUp: case UIDeviceOrientationFaceDown: default: break;
If the image is not in the desired orientation, add 90 to all of the above imageRotatedByDegrees arguments (i.e. if it is 0, then it is 0 + 90) or as needed.
EDIT 1
UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];