2012-07-26 54 views
32

我想要做的是從我的相機拍攝一張快照,發送給服務器,然後服務器發送給我一個viewController上的圖像。如果圖像處於肖像模式,則圖像在屏幕上顯示效果良好,但如果圖像是在橫向模式下拍攝的,則圖像在屏幕上顯示爲拉伸(因爲它會嘗試以縱向模式顯示!)。我不知道如何解決這個問題,但我想一個解決方案是首先檢查圖像是在縱向/橫向模式,然後如果是在橫向模式下旋轉90度,然後再在屏幕上顯示。 那我該怎麼做?如何在iOS上將圖像旋轉90度?

+0

可能重複[上傳後的iOS UIImagePickerController結果圖像方向](http:// stackove rflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload) – 2015-04-03 00:11:44

+0

我有完全相同的問題,我解決了它使用此響應中提出的類別:http://stackoverflow.com/a/5427890/1327557 它管理所有情況(旋轉和鏡像) – 2015-04-03 00:17:28

回答

95
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2); 
+0

是不是'M_PI/2'會導致不需要的類型轉換爲int並導致精度下降? – eremzeit 2014-02-09 10:22:40

+0

我發現這使我的圖像旋轉45度時,在常規視圖上使用 – hdost 2015-02-10 14:52:33

+3

'雙'不可轉換爲'CGFloat' - 我得到這個錯誤 cell.arrowImageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) - 強制類型投影修補程序 – 2015-03-27 19:44:33

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

} 
+0

不適用於任意度數。 – 2017-01-20 20:42:54

+0

如果圖像具有比例因子,此答案會降低圖像分辨率。除此之外,這是一個很好的答案。添加下一個代碼並使用寬度和高度變量而不是image.size.width和image.size.height:CGFloat width = image.size.width * image.scale,height = image.size.height * image.scale; – Borzh 2017-04-11 20:23:12

+0

或更容易,使用UIGraphicsBeginImageContextWithOptions(rotatedSize,NO,image.scale),而不是UIGraphicsBeginImageContext() – Borzh 2017-04-11 20:33:40

8

只需將此代碼添加到圖像。

Image.transform=CGAffineTransformMakeRotation(M_PI/2); 
25

這是任何程度的圖像旋轉的完整代碼只是把它添加到即.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 

這是蘋果的SquareCam示例代碼片段。

要調用上述方法只需要使用下面的代碼

UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees]; 

這裏平方是一個UIImagerotationDegrees是一個flote的ivar轉動該度

+1

此代碼不適用於肖像模式圖像的第一次。 – 2013-04-04 08:20:26

+1

對於上面的代碼工作,您還需要添加: static CGFloat DegreesToRadians(CGFloat度){return degrees * M_PI/180;}; – ddiego 2014-02-01 00:27:36

+0

@Vishu據我所知,它取決於圖像大小的內存和完成後,它釋放佔用的內存 – 2015-10-30 05:19:38

6

我得到的圖像在XCode 6.3中嘗試這個錯誤Swift 1.2

self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2); 

你應該試試這個強制類型轉換解決:

self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)); 
0

這裏是雨燕2.2版本的iOSDev答案(在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; 

} 
3

斯威夫特3版本:

imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2)) 
7

Swift 3和Swift 4使用.pi而不是

例如:

//rotate 270 degrees 
myImageView.transform = CGAffineTransform(rotationAngle: (.pi * 1.5)) 

//rotate 90 degrees 
myImageView.transform = CGAffineTransform(rotationAngle: (.pi/2)) 
2

Swift 4 | Xcode的9語法(請注意,此代碼順時針旋轉一個UIImageView 90度,而不是一個UIImage):

myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2)) 
0

斯威夫特4+。

arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2) 

Note: angle is in radian

。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) 

     } 
     }) 
    } 

輸出:的

enter image description here