[英]How to rotate an image 90 degrees on iOS?
我想要做的是從我的相機拍攝快照,將其發送到服務器,然后服務器將圖像發送回 viewController。 如果圖像處於縱向模式,則圖像在屏幕上的顯示效果很好,但是如果圖像是在橫向模式下拍攝的,則圖像在屏幕上會顯得拉長(因為它試圖在縱向模式下出現!)。 我不知道如何解決這個問題,但我想一個解決方案是首先檢查圖像是否處於縱向/橫向模式,然后如果它處於橫向模式,則在將其顯示在屏幕上之前將其旋轉 90 度。 那我怎么能這樣做呢?
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
這是 Apple 的SquareCam示例的代碼片段。
要調用上述方法只需使用以下代碼
UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
這里的正方形是一個UIImage
而rotationDegrees是一個flote
ivar 來旋轉該度數的圖像
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)
這是 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 和沒有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;
如果您要保存圖像,則需要旋轉圖片上下文而不是視圖。 我已經使用了上面提供的示例,但它們在圖像居中時效果不佳。 所以我已經增強和修復如下:
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
}
在Swift 5中在Image
級別執行此操作:
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.