How to rotate an image 90 degrees on iOS?
What I want to do is take a snapshot from my camera , send it to a server and then the server sends me back the image on a viewController. If the image is in portrait mode the image appears well on screen , however if the image was taken in landscape mode the image appears streched on the screen(as it tries to appear on portrait mode!). I dont know how to fix this but i guess one solution is first to check if the image is in portrait/landscape mode and then if it is on landscape mode rotate it by 90degrees before showing it on screen.
So how could i do that?
iphone objective-c ios rotation
add a comment |
What I want to do is take a snapshot from my camera , send it to a server and then the server sends me back the image on a viewController. If the image is in portrait mode the image appears well on screen , however if the image was taken in landscape mode the image appears streched on the screen(as it tries to appear on portrait mode!). I dont know how to fix this but i guess one solution is first to check if the image is in portrait/landscape mode and then if it is on landscape mode rotate it by 90degrees before showing it on screen.
So how could i do that?
iphone objective-c ios rotation
possible duplicate of iOS UIImagePickerController result image orientation after upload
– Axel Guilmin
Apr 3 '15 at 0:11
I had the exact same problem and I solved it using the category proposed in this response : stackoverflow.com/a/5427890/1327557 It manage all cases (rotations and miroring)
– Axel Guilmin
Apr 3 '15 at 0:17
add a comment |
What I want to do is take a snapshot from my camera , send it to a server and then the server sends me back the image on a viewController. If the image is in portrait mode the image appears well on screen , however if the image was taken in landscape mode the image appears streched on the screen(as it tries to appear on portrait mode!). I dont know how to fix this but i guess one solution is first to check if the image is in portrait/landscape mode and then if it is on landscape mode rotate it by 90degrees before showing it on screen.
So how could i do that?
iphone objective-c ios rotation
What I want to do is take a snapshot from my camera , send it to a server and then the server sends me back the image on a viewController. If the image is in portrait mode the image appears well on screen , however if the image was taken in landscape mode the image appears streched on the screen(as it tries to appear on portrait mode!). I dont know how to fix this but i guess one solution is first to check if the image is in portrait/landscape mode and then if it is on landscape mode rotate it by 90degrees before showing it on screen.
So how could i do that?
iphone objective-c ios rotation
iphone objective-c ios rotation
edited Jul 26 '12 at 11:12
IronManGill
7,13722248
7,13722248
asked Jul 26 '12 at 10:42
user1511244user1511244
3003815
3003815
possible duplicate of iOS UIImagePickerController result image orientation after upload
– Axel Guilmin
Apr 3 '15 at 0:11
I had the exact same problem and I solved it using the category proposed in this response : stackoverflow.com/a/5427890/1327557 It manage all cases (rotations and miroring)
– Axel Guilmin
Apr 3 '15 at 0:17
add a comment |
possible duplicate of iOS UIImagePickerController result image orientation after upload
– Axel Guilmin
Apr 3 '15 at 0:11
I had the exact same problem and I solved it using the category proposed in this response : stackoverflow.com/a/5427890/1327557 It manage all cases (rotations and miroring)
– Axel Guilmin
Apr 3 '15 at 0:17
possible duplicate of iOS UIImagePickerController result image orientation after upload
– Axel Guilmin
Apr 3 '15 at 0:11
possible duplicate of iOS UIImagePickerController result image orientation after upload
– Axel Guilmin
Apr 3 '15 at 0:11
I had the exact same problem and I solved it using the category proposed in this response : stackoverflow.com/a/5427890/1327557 It manage all cases (rotations and miroring)
– Axel Guilmin
Apr 3 '15 at 0:17
I had the exact same problem and I solved it using the category proposed in this response : stackoverflow.com/a/5427890/1327557 It manage all cases (rotations and miroring)
– Axel Guilmin
Apr 3 '15 at 0:17
add a comment |
11 Answers
11
active
oldest
votes
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
Swift 4:
self.imageview.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
Isn'tM_PI/2
going to cause an unwanted type casting into an int and subsequent loss of accuracy?
– eremzeit
Feb 9 '14 at 10:22
I found that this rotated my image 45 degrees when using on a regular view
– hdost
Feb 10 '15 at 14:52
3
'Double' is not convertible to 'CGFloat' - I got this error cell.arrowImageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) - force type cast fix
– Dustin Williams
Mar 27 '15 at 19:44
add a comment |
This is the complete code for rotation of image to any degree just add it to appropriate file ie in .m as below where you want to use the image processing
for .m
@interface UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
@end
@implementation UIImage (RotationMethods)
static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
// calculate the size of the rotated view's 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
This is the code snippet form apple's SquareCam example.
To call the above method just use the below code
UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
Here the square is one UIImage
and rotationDegrees is one flote
ivar to rotate the image that degrees
1
this code does not work for portrait mode image for first time.
– Shreesh Garg
Apr 4 '13 at 8:20
1
For the above code to work, you also need to add: static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
– ddiego
Feb 1 '14 at 0:27
@Vishu As I know it taking memory respective to image size and after done it released the occupied memory
– The iOSDev
Oct 30 '15 at 5:19
add a comment |
Swift 3 and Swift 4 use .pi instead
eg:
//rotate 90 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi / 2)
//rotate 180 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi)
//rotate 270 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi * 1.5)
how do i change to direction of the rotation?
– Ben Shabat
Aug 29 '18 at 9:11
@BenShabat pass in a negative value.
– terrafirma9
Sep 11 '18 at 14:36
add a comment |
- (UIImage *)rotateImage:(UIImage*)image byDegree:(CGFloat)degrees
{
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
[rotatedViewBox release];
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width, rotatedSize.height);
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-image.size.width, -image.size.height, image.size.width, image.size.height), [image CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Doesn't work with arbitrary number of degrees.
– Dielson Sales
Jan 20 '17 at 20:42
This answer lowers image resolution, if image has scale factor. Besides that this is a great answer. Add next code and use width and height variables instead of image.size.width and image.size.height: CGFloat width = image.size.width * image.scale, height = image.size.height * image.scale;
– Borzh
Apr 11 '17 at 20:23
Or easier, use UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, image.scale) instead of UIGraphicsBeginImageContext()
– Borzh
Apr 11 '17 at 20:33
add a comment |
Simply add this code to the image.
Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
add a comment |
I got an error trying this in XCode 6.3 Swift 1.2
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
You should try this to force type cast fix:
self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
add a comment |
Swift 3 version:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
add a comment |
Swift 4 | Xcode 9 syntax (please note that this code rotates a UIImageView 90 degrees clockwise, not a UIImage):
myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
add a comment |
Swift 4+.
arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2)
Note: angle is in radian
.pi = 180 degree
.pi/2 = 90 degree
If you want to add with animation
@IBAction func applyTransForm(sender: UIButton) {
sender.isSelected = !sender.isSelected
UIView.animate(withDuration: 1, animations: {
if sender.isSelected {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: .pi)
} else {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: 0)
}
})
}
Output:
add a comment |
Here is the Swift 2.2 version of The iOSDev answer (in UIImage extension):
func degreesToRadians(degrees: CGFloat) -> CGFloat {
return degrees * CGFloat(M_PI) / 180
}
func imageRotatedByDegrees(degrees: CGFloat) -> UIImage {
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRectMake(0,0,self.size.width, self.size.height))
let t = CGAffineTransformMakeRotation(self.degreesToRadians(degrees))
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let 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);
// 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);
let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
add a comment |
It´s best only use the bitmap. In my case use a UIImage that is in reference.image and I converted image at base64String. Without CGAffineTransformMakeRotation and without rotatedViewBox.transform
var rotatedSize = reference.image.size;
//Create bitmap context
UIGraphicsBeginImageContext(rotatedSize);
var bitmap = UIGraphicsGetCurrentContext();
//move origin to the middle of the image for rotation
CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2);
//rotate image context
CGContextRotateCTM(bitmap, 1.5708);
CGContextDrawImage(bitmap, CGRectMake(-reference.image.size.width / 2, -reference.image.size.height / 2, reference.image.size.width, reference.image.size.height), reference.image.CGImage);
var rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
var base64Image = UIImageJPEGRepresentation(rotatedImage, compression).base64EncodedStringWithOptions(0);
UIGraphicsEndImageContext();
return base64Image;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11667565%2fhow-to-rotate-an-image-90-degrees-on-ios%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
Swift 4:
self.imageview.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
Isn'tM_PI/2
going to cause an unwanted type casting into an int and subsequent loss of accuracy?
– eremzeit
Feb 9 '14 at 10:22
I found that this rotated my image 45 degrees when using on a regular view
– hdost
Feb 10 '15 at 14:52
3
'Double' is not convertible to 'CGFloat' - I got this error cell.arrowImageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) - force type cast fix
– Dustin Williams
Mar 27 '15 at 19:44
add a comment |
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
Swift 4:
self.imageview.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
Isn'tM_PI/2
going to cause an unwanted type casting into an int and subsequent loss of accuracy?
– eremzeit
Feb 9 '14 at 10:22
I found that this rotated my image 45 degrees when using on a regular view
– hdost
Feb 10 '15 at 14:52
3
'Double' is not convertible to 'CGFloat' - I got this error cell.arrowImageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) - force type cast fix
– Dustin Williams
Mar 27 '15 at 19:44
add a comment |
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
Swift 4:
self.imageview.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
Swift 4:
self.imageview.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
edited Nov 13 '18 at 4:17
Jayprakash Dubey
24.3k9123132
24.3k9123132
answered Jul 26 '12 at 10:45
mandeep-dhimanmandeep-dhiman
1,89821427
1,89821427
Isn'tM_PI/2
going to cause an unwanted type casting into an int and subsequent loss of accuracy?
– eremzeit
Feb 9 '14 at 10:22
I found that this rotated my image 45 degrees when using on a regular view
– hdost
Feb 10 '15 at 14:52
3
'Double' is not convertible to 'CGFloat' - I got this error cell.arrowImageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) - force type cast fix
– Dustin Williams
Mar 27 '15 at 19:44
add a comment |
Isn'tM_PI/2
going to cause an unwanted type casting into an int and subsequent loss of accuracy?
– eremzeit
Feb 9 '14 at 10:22
I found that this rotated my image 45 degrees when using on a regular view
– hdost
Feb 10 '15 at 14:52
3
'Double' is not convertible to 'CGFloat' - I got this error cell.arrowImageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) - force type cast fix
– Dustin Williams
Mar 27 '15 at 19:44
Isn't
M_PI/2
going to cause an unwanted type casting into an int and subsequent loss of accuracy?– eremzeit
Feb 9 '14 at 10:22
Isn't
M_PI/2
going to cause an unwanted type casting into an int and subsequent loss of accuracy?– eremzeit
Feb 9 '14 at 10:22
I found that this rotated my image 45 degrees when using on a regular view
– hdost
Feb 10 '15 at 14:52
I found that this rotated my image 45 degrees when using on a regular view
– hdost
Feb 10 '15 at 14:52
3
3
'Double' is not convertible to 'CGFloat' - I got this error cell.arrowImageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) - force type cast fix
– Dustin Williams
Mar 27 '15 at 19:44
'Double' is not convertible to 'CGFloat' - I got this error cell.arrowImageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) - force type cast fix
– Dustin Williams
Mar 27 '15 at 19:44
add a comment |
This is the complete code for rotation of image to any degree just add it to appropriate file ie in .m as below where you want to use the image processing
for .m
@interface UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
@end
@implementation UIImage (RotationMethods)
static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
// calculate the size of the rotated view's 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
This is the code snippet form apple's SquareCam example.
To call the above method just use the below code
UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
Here the square is one UIImage
and rotationDegrees is one flote
ivar to rotate the image that degrees
1
this code does not work for portrait mode image for first time.
– Shreesh Garg
Apr 4 '13 at 8:20
1
For the above code to work, you also need to add: static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
– ddiego
Feb 1 '14 at 0:27
@Vishu As I know it taking memory respective to image size and after done it released the occupied memory
– The iOSDev
Oct 30 '15 at 5:19
add a comment |
This is the complete code for rotation of image to any degree just add it to appropriate file ie in .m as below where you want to use the image processing
for .m
@interface UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
@end
@implementation UIImage (RotationMethods)
static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
// calculate the size of the rotated view's 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
This is the code snippet form apple's SquareCam example.
To call the above method just use the below code
UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
Here the square is one UIImage
and rotationDegrees is one flote
ivar to rotate the image that degrees
1
this code does not work for portrait mode image for first time.
– Shreesh Garg
Apr 4 '13 at 8:20
1
For the above code to work, you also need to add: static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
– ddiego
Feb 1 '14 at 0:27
@Vishu As I know it taking memory respective to image size and after done it released the occupied memory
– The iOSDev
Oct 30 '15 at 5:19
add a comment |
This is the complete code for rotation of image to any degree just add it to appropriate file ie in .m as below where you want to use the image processing
for .m
@interface UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
@end
@implementation UIImage (RotationMethods)
static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
// calculate the size of the rotated view's 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
This is the code snippet form apple's SquareCam example.
To call the above method just use the below code
UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
Here the square is one UIImage
and rotationDegrees is one flote
ivar to rotate the image that degrees
This is the complete code for rotation of image to any degree just add it to appropriate file ie in .m as below where you want to use the image processing
for .m
@interface UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
@end
@implementation UIImage (RotationMethods)
static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
// calculate the size of the rotated view's 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
This is the code snippet form apple's SquareCam example.
To call the above method just use the below code
UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
Here the square is one UIImage
and rotationDegrees is one flote
ivar to rotate the image that degrees
edited Apr 21 '14 at 8:57
Stunner
7,8551069124
7,8551069124
answered Jul 26 '12 at 10:59
The iOSDevThe iOSDev
4,56973671
4,56973671
1
this code does not work for portrait mode image for first time.
– Shreesh Garg
Apr 4 '13 at 8:20
1
For the above code to work, you also need to add: static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
– ddiego
Feb 1 '14 at 0:27
@Vishu As I know it taking memory respective to image size and after done it released the occupied memory
– The iOSDev
Oct 30 '15 at 5:19
add a comment |
1
this code does not work for portrait mode image for first time.
– Shreesh Garg
Apr 4 '13 at 8:20
1
For the above code to work, you also need to add: static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
– ddiego
Feb 1 '14 at 0:27
@Vishu As I know it taking memory respective to image size and after done it released the occupied memory
– The iOSDev
Oct 30 '15 at 5:19
1
1
this code does not work for portrait mode image for first time.
– Shreesh Garg
Apr 4 '13 at 8:20
this code does not work for portrait mode image for first time.
– Shreesh Garg
Apr 4 '13 at 8:20
1
1
For the above code to work, you also need to add: static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
– ddiego
Feb 1 '14 at 0:27
For the above code to work, you also need to add: static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
– ddiego
Feb 1 '14 at 0:27
@Vishu As I know it taking memory respective to image size and after done it released the occupied memory
– The iOSDev
Oct 30 '15 at 5:19
@Vishu As I know it taking memory respective to image size and after done it released the occupied memory
– The iOSDev
Oct 30 '15 at 5:19
add a comment |
Swift 3 and Swift 4 use .pi instead
eg:
//rotate 90 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi / 2)
//rotate 180 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi)
//rotate 270 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi * 1.5)
how do i change to direction of the rotation?
– Ben Shabat
Aug 29 '18 at 9:11
@BenShabat pass in a negative value.
– terrafirma9
Sep 11 '18 at 14:36
add a comment |
Swift 3 and Swift 4 use .pi instead
eg:
//rotate 90 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi / 2)
//rotate 180 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi)
//rotate 270 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi * 1.5)
how do i change to direction of the rotation?
– Ben Shabat
Aug 29 '18 at 9:11
@BenShabat pass in a negative value.
– terrafirma9
Sep 11 '18 at 14:36
add a comment |
Swift 3 and Swift 4 use .pi instead
eg:
//rotate 90 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi / 2)
//rotate 180 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi)
//rotate 270 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi * 1.5)
Swift 3 and Swift 4 use .pi instead
eg:
//rotate 90 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi / 2)
//rotate 180 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi)
//rotate 270 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi * 1.5)
edited Sep 11 '18 at 8:53
answered Apr 13 '17 at 20:19
Jesus Adolfo RodriguezJesus Adolfo Rodriguez
1,11311231
1,11311231
how do i change to direction of the rotation?
– Ben Shabat
Aug 29 '18 at 9:11
@BenShabat pass in a negative value.
– terrafirma9
Sep 11 '18 at 14:36
add a comment |
how do i change to direction of the rotation?
– Ben Shabat
Aug 29 '18 at 9:11
@BenShabat pass in a negative value.
– terrafirma9
Sep 11 '18 at 14:36
how do i change to direction of the rotation?
– Ben Shabat
Aug 29 '18 at 9:11
how do i change to direction of the rotation?
– Ben Shabat
Aug 29 '18 at 9:11
@BenShabat pass in a negative value.
– terrafirma9
Sep 11 '18 at 14:36
@BenShabat pass in a negative value.
– terrafirma9
Sep 11 '18 at 14:36
add a comment |
- (UIImage *)rotateImage:(UIImage*)image byDegree:(CGFloat)degrees
{
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
[rotatedViewBox release];
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width, rotatedSize.height);
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-image.size.width, -image.size.height, image.size.width, image.size.height), [image CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Doesn't work with arbitrary number of degrees.
– Dielson Sales
Jan 20 '17 at 20:42
This answer lowers image resolution, if image has scale factor. Besides that this is a great answer. Add next code and use width and height variables instead of image.size.width and image.size.height: CGFloat width = image.size.width * image.scale, height = image.size.height * image.scale;
– Borzh
Apr 11 '17 at 20:23
Or easier, use UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, image.scale) instead of UIGraphicsBeginImageContext()
– Borzh
Apr 11 '17 at 20:33
add a comment |
- (UIImage *)rotateImage:(UIImage*)image byDegree:(CGFloat)degrees
{
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
[rotatedViewBox release];
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width, rotatedSize.height);
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-image.size.width, -image.size.height, image.size.width, image.size.height), [image CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Doesn't work with arbitrary number of degrees.
– Dielson Sales
Jan 20 '17 at 20:42
This answer lowers image resolution, if image has scale factor. Besides that this is a great answer. Add next code and use width and height variables instead of image.size.width and image.size.height: CGFloat width = image.size.width * image.scale, height = image.size.height * image.scale;
– Borzh
Apr 11 '17 at 20:23
Or easier, use UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, image.scale) instead of UIGraphicsBeginImageContext()
– Borzh
Apr 11 '17 at 20:33
add a comment |
- (UIImage *)rotateImage:(UIImage*)image byDegree:(CGFloat)degrees
{
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
[rotatedViewBox release];
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width, rotatedSize.height);
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-image.size.width, -image.size.height, image.size.width, image.size.height), [image CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (UIImage *)rotateImage:(UIImage*)image byDegree:(CGFloat)degrees
{
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
[rotatedViewBox release];
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width, rotatedSize.height);
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-image.size.width, -image.size.height, image.size.width, image.size.height), [image CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
edited Jul 26 '12 at 10:53
answered Jul 26 '12 at 10:44
janusfideljanusfidel
6,33932448
6,33932448
Doesn't work with arbitrary number of degrees.
– Dielson Sales
Jan 20 '17 at 20:42
This answer lowers image resolution, if image has scale factor. Besides that this is a great answer. Add next code and use width and height variables instead of image.size.width and image.size.height: CGFloat width = image.size.width * image.scale, height = image.size.height * image.scale;
– Borzh
Apr 11 '17 at 20:23
Or easier, use UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, image.scale) instead of UIGraphicsBeginImageContext()
– Borzh
Apr 11 '17 at 20:33
add a comment |
Doesn't work with arbitrary number of degrees.
– Dielson Sales
Jan 20 '17 at 20:42
This answer lowers image resolution, if image has scale factor. Besides that this is a great answer. Add next code and use width and height variables instead of image.size.width and image.size.height: CGFloat width = image.size.width * image.scale, height = image.size.height * image.scale;
– Borzh
Apr 11 '17 at 20:23
Or easier, use UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, image.scale) instead of UIGraphicsBeginImageContext()
– Borzh
Apr 11 '17 at 20:33
Doesn't work with arbitrary number of degrees.
– Dielson Sales
Jan 20 '17 at 20:42
Doesn't work with arbitrary number of degrees.
– Dielson Sales
Jan 20 '17 at 20:42
This answer lowers image resolution, if image has scale factor. Besides that this is a great answer. Add next code and use width and height variables instead of image.size.width and image.size.height: CGFloat width = image.size.width * image.scale, height = image.size.height * image.scale;
– Borzh
Apr 11 '17 at 20:23
This answer lowers image resolution, if image has scale factor. Besides that this is a great answer. Add next code and use width and height variables instead of image.size.width and image.size.height: CGFloat width = image.size.width * image.scale, height = image.size.height * image.scale;
– Borzh
Apr 11 '17 at 20:23
Or easier, use UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, image.scale) instead of UIGraphicsBeginImageContext()
– Borzh
Apr 11 '17 at 20:33
Or easier, use UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, image.scale) instead of UIGraphicsBeginImageContext()
– Borzh
Apr 11 '17 at 20:33
add a comment |
Simply add this code to the image.
Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
add a comment |
Simply add this code to the image.
Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
add a comment |
Simply add this code to the image.
Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
Simply add this code to the image.
Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
edited Jul 26 '12 at 10:53
answered Jul 26 '12 at 10:46
IronManGillIronManGill
7,13722248
7,13722248
add a comment |
add a comment |
I got an error trying this in XCode 6.3 Swift 1.2
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
You should try this to force type cast fix:
self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
add a comment |
I got an error trying this in XCode 6.3 Swift 1.2
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
You should try this to force type cast fix:
self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
add a comment |
I got an error trying this in XCode 6.3 Swift 1.2
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
You should try this to force type cast fix:
self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
I got an error trying this in XCode 6.3 Swift 1.2
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
You should try this to force type cast fix:
self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
answered May 19 '15 at 21:32
mattyUmattyU
92711316
92711316
add a comment |
add a comment |
Swift 3 version:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
add a comment |
Swift 3 version:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
add a comment |
Swift 3 version:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
Swift 3 version:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
answered Feb 3 '17 at 8:11
Dhruv KhatriDhruv Khatri
738414
738414
add a comment |
add a comment |
Swift 4 | Xcode 9 syntax (please note that this code rotates a UIImageView 90 degrees clockwise, not a UIImage):
myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
add a comment |
Swift 4 | Xcode 9 syntax (please note that this code rotates a UIImageView 90 degrees clockwise, not a UIImage):
myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
add a comment |
Swift 4 | Xcode 9 syntax (please note that this code rotates a UIImageView 90 degrees clockwise, not a UIImage):
myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
Swift 4 | Xcode 9 syntax (please note that this code rotates a UIImageView 90 degrees clockwise, not a UIImage):
myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
answered Sep 26 '17 at 15:16
xscoderxscoder
1,13321532
1,13321532
add a comment |
add a comment |
Swift 4+.
arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2)
Note: angle is in radian
.pi = 180 degree
.pi/2 = 90 degree
If you want to add with animation
@IBAction func applyTransForm(sender: UIButton) {
sender.isSelected = !sender.isSelected
UIView.animate(withDuration: 1, animations: {
if sender.isSelected {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: .pi)
} else {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: 0)
}
})
}
Output:
add a comment |
Swift 4+.
arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2)
Note: angle is in radian
.pi = 180 degree
.pi/2 = 90 degree
If you want to add with animation
@IBAction func applyTransForm(sender: UIButton) {
sender.isSelected = !sender.isSelected
UIView.animate(withDuration: 1, animations: {
if sender.isSelected {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: .pi)
} else {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: 0)
}
})
}
Output:
add a comment |
Swift 4+.
arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2)
Note: angle is in radian
.pi = 180 degree
.pi/2 = 90 degree
If you want to add with animation
@IBAction func applyTransForm(sender: UIButton) {
sender.isSelected = !sender.isSelected
UIView.animate(withDuration: 1, animations: {
if sender.isSelected {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: .pi)
} else {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: 0)
}
})
}
Output:
Swift 4+.
arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2)
Note: angle is in radian
.pi = 180 degree
.pi/2 = 90 degree
If you want to add with animation
@IBAction func applyTransForm(sender: UIButton) {
sender.isSelected = !sender.isSelected
UIView.animate(withDuration: 1, animations: {
if sender.isSelected {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: .pi)
} else {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: 0)
}
})
}
Output:
answered Feb 17 '18 at 6:29
JackJack
5,85833356
5,85833356
add a comment |
add a comment |
Here is the Swift 2.2 version of The iOSDev answer (in UIImage extension):
func degreesToRadians(degrees: CGFloat) -> CGFloat {
return degrees * CGFloat(M_PI) / 180
}
func imageRotatedByDegrees(degrees: CGFloat) -> UIImage {
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRectMake(0,0,self.size.width, self.size.height))
let t = CGAffineTransformMakeRotation(self.degreesToRadians(degrees))
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let 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);
// 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);
let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
add a comment |
Here is the Swift 2.2 version of The iOSDev answer (in UIImage extension):
func degreesToRadians(degrees: CGFloat) -> CGFloat {
return degrees * CGFloat(M_PI) / 180
}
func imageRotatedByDegrees(degrees: CGFloat) -> UIImage {
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRectMake(0,0,self.size.width, self.size.height))
let t = CGAffineTransformMakeRotation(self.degreesToRadians(degrees))
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let 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);
// 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);
let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
add a comment |
Here is the Swift 2.2 version of The iOSDev answer (in UIImage extension):
func degreesToRadians(degrees: CGFloat) -> CGFloat {
return degrees * CGFloat(M_PI) / 180
}
func imageRotatedByDegrees(degrees: CGFloat) -> UIImage {
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRectMake(0,0,self.size.width, self.size.height))
let t = CGAffineTransformMakeRotation(self.degreesToRadians(degrees))
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let 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);
// 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);
let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Here is the Swift 2.2 version of The iOSDev answer (in UIImage extension):
func degreesToRadians(degrees: CGFloat) -> CGFloat {
return degrees * CGFloat(M_PI) / 180
}
func imageRotatedByDegrees(degrees: CGFloat) -> UIImage {
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRectMake(0,0,self.size.width, self.size.height))
let t = CGAffineTransformMakeRotation(self.degreesToRadians(degrees))
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let 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);
// 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);
let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
answered Jun 21 '16 at 15:14
Beninho85Beninho85
2,3011620
2,3011620
add a comment |
add a comment |
It´s best only use the bitmap. In my case use a UIImage that is in reference.image and I converted image at base64String. Without CGAffineTransformMakeRotation and without rotatedViewBox.transform
var rotatedSize = reference.image.size;
//Create bitmap context
UIGraphicsBeginImageContext(rotatedSize);
var bitmap = UIGraphicsGetCurrentContext();
//move origin to the middle of the image for rotation
CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2);
//rotate image context
CGContextRotateCTM(bitmap, 1.5708);
CGContextDrawImage(bitmap, CGRectMake(-reference.image.size.width / 2, -reference.image.size.height / 2, reference.image.size.width, reference.image.size.height), reference.image.CGImage);
var rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
var base64Image = UIImageJPEGRepresentation(rotatedImage, compression).base64EncodedStringWithOptions(0);
UIGraphicsEndImageContext();
return base64Image;
add a comment |
It´s best only use the bitmap. In my case use a UIImage that is in reference.image and I converted image at base64String. Without CGAffineTransformMakeRotation and without rotatedViewBox.transform
var rotatedSize = reference.image.size;
//Create bitmap context
UIGraphicsBeginImageContext(rotatedSize);
var bitmap = UIGraphicsGetCurrentContext();
//move origin to the middle of the image for rotation
CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2);
//rotate image context
CGContextRotateCTM(bitmap, 1.5708);
CGContextDrawImage(bitmap, CGRectMake(-reference.image.size.width / 2, -reference.image.size.height / 2, reference.image.size.width, reference.image.size.height), reference.image.CGImage);
var rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
var base64Image = UIImageJPEGRepresentation(rotatedImage, compression).base64EncodedStringWithOptions(0);
UIGraphicsEndImageContext();
return base64Image;
add a comment |
It´s best only use the bitmap. In my case use a UIImage that is in reference.image and I converted image at base64String. Without CGAffineTransformMakeRotation and without rotatedViewBox.transform
var rotatedSize = reference.image.size;
//Create bitmap context
UIGraphicsBeginImageContext(rotatedSize);
var bitmap = UIGraphicsGetCurrentContext();
//move origin to the middle of the image for rotation
CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2);
//rotate image context
CGContextRotateCTM(bitmap, 1.5708);
CGContextDrawImage(bitmap, CGRectMake(-reference.image.size.width / 2, -reference.image.size.height / 2, reference.image.size.width, reference.image.size.height), reference.image.CGImage);
var rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
var base64Image = UIImageJPEGRepresentation(rotatedImage, compression).base64EncodedStringWithOptions(0);
UIGraphicsEndImageContext();
return base64Image;
It´s best only use the bitmap. In my case use a UIImage that is in reference.image and I converted image at base64String. Without CGAffineTransformMakeRotation and without rotatedViewBox.transform
var rotatedSize = reference.image.size;
//Create bitmap context
UIGraphicsBeginImageContext(rotatedSize);
var bitmap = UIGraphicsGetCurrentContext();
//move origin to the middle of the image for rotation
CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2);
//rotate image context
CGContextRotateCTM(bitmap, 1.5708);
CGContextDrawImage(bitmap, CGRectMake(-reference.image.size.width / 2, -reference.image.size.height / 2, reference.image.size.width, reference.image.size.height), reference.image.CGImage);
var rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
var base64Image = UIImageJPEGRepresentation(rotatedImage, compression).base64EncodedStringWithOptions(0);
UIGraphicsEndImageContext();
return base64Image;
answered Jan 19 at 16:22
Luis OrtizLuis Ortiz
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11667565%2fhow-to-rotate-an-image-90-degrees-on-ios%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
possible duplicate of iOS UIImagePickerController result image orientation after upload
– Axel Guilmin
Apr 3 '15 at 0:11
I had the exact same problem and I solved it using the category proposed in this response : stackoverflow.com/a/5427890/1327557 It manage all cases (rotations and miroring)
– Axel Guilmin
Apr 3 '15 at 0:17