如何在iOS上将图像旋转90度?

use*_*244 43 iphone objective-c rotation ios

我想要做的是从我的相机拍摄快照,将其发送到服务器,然后服务器将我的图像发送回viewController.如果图像处于纵向模式,则图像在屏幕上显示良好,但是如果图像是以横向模式拍摄的,则图像在屏幕上显示为拉伸(因为它试图以纵向模式显示!).我不知道如何解决这个问题,但我猜一个解决方案是首先检查图像是否处于纵向/横向模式,然后如果处于横向模式,则在将其显示在屏幕上之前将其旋转90度.那我该怎么办呢?

man*_*man 115

self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
Run Code Online (Sandbox Code Playgroud)

斯威夫特4:

self.imageview.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
Run Code Online (Sandbox Code Playgroud)

  • 'Double'不能转换为'CGFloat' - 我得到了这个错误cell.arrowImageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) - 强制类型转换修复 (3认同)

The*_*Dev 29

这是将图像旋转到任何程度的完整代码,只需将其添加到适当的文件,即在.m中,如下所示,您要使用图像处理

对于.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
Run Code Online (Sandbox Code Playgroud)

这是Apple的SquareCam示例的代码片段.

要调用上述方法,只需使用以下代码即可

UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
Run Code Online (Sandbox Code Playgroud)

这里的正方形是一个,UIImagerotationDegrees是一个flote用于旋转度数的图像的ivar


Jes*_*uez 15

Swift 3和Swift 4使用.pi代替

例如:

//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)
Run Code Online (Sandbox Code Playgroud)


jan*_*del 12

- (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;

}
Run Code Online (Sandbox Code Playgroud)


Iro*_*ill 9

只需将此代码添加到图像中即可.

Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
Run Code Online (Sandbox Code Playgroud)


mat*_*tyU 6

我在XCode 6.3 Swift 1.2中尝试了这个错误

self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
Run Code Online (Sandbox Code Playgroud)

你应该尝试这个强制类型转换修复:

self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
Run Code Online (Sandbox Code Playgroud)