我想要做的是从我的相机拍摄快照,将其发送到服务器,然后服务器将我发送回 viewController 上的图像。如果图像处于纵向模式,则图像在屏幕上显示良好,但是如果图像是在横向模式下拍摄的,则图像在屏幕上会出现拉伸(因为它试图以纵向模式出现!)。我不知道如何解决这个问题,但我想一个解决方案是首先检查图像是否处于纵向/横向模式,然后如果它处于横向模式,则将其旋转 90 度,然后再将其显示在屏幕上。那么我该怎么做呢?
14 回答
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
斯威夫特 4+:
self.imageview.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
这是将图像旋转到任何程度的完整代码,只需将其添加到适当的文件中,即 .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
这是苹果SquareCam示例的代码片段。
要调用上述方法,只需使用以下代码
UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
这里的正方形是 1 UIImage
,rotationDegrees是 1flote
以将图像旋转角度
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)
- (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;
}
只需将此代码添加到图像中。
Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
我在 XCode 6.3 Swift 1.2 中尝试此操作时出错
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
你应该试试这个来强制类型转换修复:
self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
斯威夫特 4 编辑
imageview.transform = CGAffineTransform(rotationAngle: .pi)
斯威夫特 3 版本:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
斯威夫特 4+。
arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2)
.pi = 180 度
.pi/2 = 90 度
如果要添加动画
@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)
}
})
}
输出:
斯威夫特 4 | Xcode 9 语法(请注意,此代码将 UIImageView 顺时针旋转 90 度,而不是 UIImage):
myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage {
let size = oldImage.size
UIGraphicsBeginImageContext(size)
let bitmap: CGContext = UIGraphicsGetCurrentContext()!
//Move the origin to the middle of the image so we will rotate and scale around the center.
bitmap.translateBy(x: size.width / 2, y: size.height / 2)
//Rotate the image context
bitmap.rotate(by: (degrees * CGFloat(Double.pi / 180)))
//Now, draw the rotated/scaled image into the context
bitmap.scaleBy(x: 1.0, y: -1.0)
let origin = CGPoint(x: -size.width / 2, y: -size.width / 2)
bitmap.draw(oldImage.cgImage!, in: CGRect(origin: origin, size: size))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
函数的使用
imageRotatedByDegrees(oldImage: yourImage, deg: degree)
在Swift 5Image
的关卡中执行此操作:
extension UIImage{
func imageRotated(by radian: CGFloat) -> UIImage{
let rotatedSize = CGRect(origin: .zero, size: size)
.applying(CGAffineTransform(rotationAngle: radian))
.integral.size
UIGraphicsBeginImageContext(rotatedSize)
if let context = UIGraphicsGetCurrentContext() {
let origin = CGPoint(x: rotatedSize.width / 2.0,
y: rotatedSize.height / 2.0)
context.translateBy(x: origin.x, y: origin.y)
context.rotate(by: radian)
draw(in: CGRect(x: -origin.y, y: -origin.x,
width: size.width, height: size.height))
let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return rotatedImage ?? self
}
return self
}
}
这是 iOSDev 答案的 Swift 2.2 版本(在 UIImage 扩展中):
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;
}
最好只使用位图。在我的情况下,使用 reference.image 中的 UIImage,我在 base64String 处转换了图像。没有 CGAffineTransformMakeRotation 和没有 rotateViewBox.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;
如果您要保存图像,则需要旋转图片上下文而不是视图。我已经使用了上面提供的示例,但是在将图像居中时它们效果不佳。所以我已经增强和修复如下:
func imageRotatedByDegrees(deg degrees: CGFloat) -> UIImage {
UIGraphicsBeginImageContext(CGSize(width: self.size.height, height: self.size.width))
let bitmap: CGContext = UIGraphicsGetCurrentContext()!
// Move the origin to the middle of the image so we will rotate and scale around the center.
bitmap.translateBy(x: self.size.width / 2, y: self.size.height / 2)
// Rotate the image context
bitmap.rotate(by: (degrees * CGFloat(Double.pi / 180)))
// Now, draw the rotated/scaled image into the context
bitmap.scaleBy(x: 1.0, y: -1.0)
let origin = CGPoint(x: -self.size.height / 2, y: -self.size.width / 2)
bitmap.draw(self.cgImage!, in: CGRect(origin: origin, size: CGSize(width: self.size.width, height: self.size.height)))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}