我想要做的是從我的相機拍攝一張快照,發送給服務器,然後服務器發送給我一個viewController上的圖像。如果圖像處於肖像模式,則圖像在屏幕上顯示效果良好,但如果圖像是在橫向模式下拍攝的,則圖像在屏幕上顯示爲拉伸(因爲它會嘗試以縱向模式顯示!)。我不知道如何解決這個問題,但我想一個解決方案是首先檢查圖像是在縱向/橫向模式,然後如果是在橫向模式下旋轉90度,然後再在屏幕上顯示。 那我該怎麼做?如何在iOS上將圖像旋轉90度?
回答
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
- (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;
}
不適用於任意度數。 – 2017-01-20 20:42:54
如果圖像具有比例因子,此答案會降低圖像分辨率。除此之外,這是一個很好的答案。添加下一個代碼並使用寬度和高度變量而不是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
或更容易,使用UIGraphicsBeginImageContextWithOptions(rotatedSize,NO,image.scale),而不是UIGraphicsBeginImageContext() – Borzh 2017-04-11 20:33:40
只需將此代碼添加到圖像。
Image.transform=CGAffineTransformMakeRotation(M_PI/2);
這是任何程度的圖像旋轉的完整代碼只是把它添加到即.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];
這裏平方是一個UIImage
和rotationDegrees是一個flote
的ivar轉動該度
此代碼不適用於肖像模式圖像的第一次。 – 2013-04-04 08:20:26
對於上面的代碼工作,您還需要添加: static CGFloat DegreesToRadians(CGFloat度){return degrees * M_PI/180;}; – ddiego 2014-02-01 00:27:36
@Vishu據我所知,它取決於圖像大小的內存和完成後,它釋放佔用的內存 – 2015-10-30 05:19:38
我得到的圖像在XCode 6.3中嘗試這個錯誤Swift 1.2
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
你應該試試這個強制類型轉換解決:
self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
這裏是雨燕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版本:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
Swift 3和Swift 4使用.pi而不是
例如:
//rotate 270 degrees
myImageView.transform = CGAffineTransform(rotationAngle: (.pi * 1.5))
//rotate 90 degrees
myImageView.transform = CGAffineTransform(rotationAngle: (.pi/2))
Swift 4 | Xcode的9語法(請注意,此代碼順時針旋轉一個UIImageView 90度,而不是一個UIImage):
myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.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)
}
})
}
輸出:的
- 1. 在iOS中將文本旋轉90度
- 2. 在Chrome中旋轉90度的圖像
- 3. 在C中旋轉PPM圖像90度
- 4. ImagePicker將iOS相機圖像旋轉90度
- 5. matrix.postRotate(90)在圖像旋轉
- 6. 如何將UIImage和圖像數據旋轉90度
- 7. 旋轉90度後,如何將圖像添加到CALayer背面?
- 8. 將2D像素陣列旋轉90度
- 9. UIImagePNGRepresentation問題? /圖像旋轉90度
- 10. 順時針旋轉90度的圖像
- 11. Kineticjs - 順時針旋轉圖像90度
- 12. OpenCV中的圖像旋轉(90度)
- 13. 如何旋轉TeeChart 90度
- 14. 如何旋轉StageVideo 90度
- 15. 根據其他圖像的位置將圖像旋轉90度
- 16. 旋轉90度/ -90度的圖像模糊jquery rotate
- 17. 如何將輸出旋轉90度?
- 18. 如何在iOS上將圖像更改爲90度?
- 19. 圖片90度旋轉AVCam
- 20. C#旋轉位圖90度
- 21. 地圖旋轉了90度?
- 22. 從iOS中的URL加載後旋轉90度的圖像?
- 23. 在HTML5上旋轉90度的圖像Canvas
- 24. iOS Swift 3圖像左轉90度
- 25. 90度圖像旋轉和重置EXIF旋轉
- 26. 如何在AmCharts中將漏斗圖旋轉90度?
- 27. 旋轉CALayer 90度?
- 28. 旋轉臺90度
- 29. CGAffineTransform旋轉90度
- 30. System.Drawing.Graphics Drawimage將從IOS獲取的圖片旋轉到90度
可能重複[上傳後的iOS UIImagePickerController結果圖像方向](http:// stackove rflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload) – 2015-04-03 00:11:44
我有完全相同的問題,我解決了它使用此響應中提出的類別:http://stackoverflow.com/a/5427890/1327557 它管理所有情況(旋轉和鏡像) – 2015-04-03 00:17:28