Những gì tôi muốn làm là chụp nhanh từ máy ảnh của tôi, gửi nó đến một máy chủ và sau đó máy chủ gửi cho tôi trở lại hình ảnh trên một viewController. Nếu hình ảnh ở chế độ dọc, hình ảnh xuất hiện tốt trên màn hình, tuy nhiên nếu hình ảnh được chụp ở chế độ ngang, hình ảnh sẽ xuất hiện trên màn hình (vì nó sẽ xuất hiện ở chế độ dọc). Tôi không biết làm thế nào để sửa lỗi này nhưng tôi đoán một giải pháp là đầu tiên để kiểm tra xem hình ảnh là trong chế độ chân dung/phong cảnh và sau đó nếu nó là chế độ phong cảnh xoay nó bằng 90 độ trước khi hiển thị nó trên màn hình. Vậy làm thế nào tôi có thể làm điều đó?Làm cách nào để xoay hình ảnh 90 độ trên iOS?
Trả lời
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
Không phải 'M_PI/2' sẽ gây ra một loại không mong muốn đúc vào một int và sau đó mất chính xác? – eremzeit
Tôi thấy rằng điều này xoay hình ảnh của tôi 45 độ khi sử dụng trên chế độ xem thông thường – hdost
'Đôi' không thể chuyển thành 'CGFloat' - Tôi gặp lỗi này cell.arrowImageView? .transform = CGAffineTransformMakeRotation (CGFloat (M_PI_2)) - loại lực cast fix –
- (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;
}
Không hoạt động với số độ tùy ý. –
Câu trả lời này làm giảm độ phân giải hình ảnh, nếu hình ảnh có yếu tố tỷ lệ. Bên cạnh đó đây là một câu trả lời tuyệt vời. Thêm mã tiếp theo và sử dụng các biến chiều rộng và chiều cao thay vì image.size.width và image.size.height: CGFloat width = image.size.width * image.scale, height = image.size.height * image.scale; – Borzh
Hoặc dễ dàng hơn, sử dụng UIGraphicsBeginImageContextWithOptions (rotatedSize, NO, image.scale) thay vì UIGraphicsBeginImageContext() – Borzh
Đơn giản chỉ cần thêm mã này vào hình ảnh.
Image.transform=CGAffineTransformMakeRotation(M_PI/2);
Đây là mã hoàn chỉnh cho chuyển động quay của hình ảnh với bất kỳ mức độ chỉ cần thêm nó vào tập tin thích hợp tức là trong .m như bên dưới nơi bạn muốn sử dụng xử lý hình ảnh
cho .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
Đây là mẫu đoạn mã của ví dụ SquareCam của apple.
Để gọi các phương pháp trên chỉ cần sử dụng mã dưới đây
UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
Ở đây vuông là một UIImage
và rotationDegrees là một flote
Ivar để xoay hình ảnh rằng các văn bằng
mã này không hoạt động cho hình ảnh chế độ dọc cho lần đầu tiên. –
Để mã trên hoạt động, bạn cũng cần phải thêm: static CGFloat DegreesToRadians (CGFloat degrees) {return degrees * M_PI/180;}; – ddiego
@Vishu Như tôi biết nó lấy bộ nhớ tương ứng với kích thước hình ảnh và sau khi thực hiện nó phát hành bộ nhớ bị chiếm đóng –
tôi có một lỗi cố gắng điều này trong XCode 6.3 Swift 1.2
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
Bạn nên cố gắng này để buộc sửa chữa loại dàn diễn viên:
self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2));
Đây là 2.2 phiên bản Swift của The iOSDev câu trả lời (trong phần mở rộng 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;
}
Swift 3 phiên bản:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
Swift 3 và Swift 4 sử dụng .pi thay vì
ví dụ:
//rotate 270 degrees
myImageView.transform = CGAffineTransform(rotationAngle: (.pi * 1.5))
//rotate 90 degrees
myImageView.transform = CGAffineTransform(rotationAngle: (.pi/2))
Swift 4 | Xcode 9 cú pháp (xin lưu ý rằng mã này quay một UIImageView 90 độ theo chiều kim đồng, không phải là một UIImage):
myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
Swift 4+.
arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2)
.pi = 180 độ
.pi/2 = 90 độ
Nếu bạn muốn thêm với hình ảnh động
@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)
}
})
}
Output:
- 1. xoay hình ảnh 90 độ trong java
- 2. Android xoay ảnh 90 độ (chụp bằng máy ảnh)
- 3. C# xoay bitmap 90 độ
- 4. Phonegap/Cordova xoay hình ảnh giật gân qua 90 độ khi tải trên iphone
- 5. Xoay bitmap 90 độ
- 6. Cách dễ nhất để xoay 90 độ một hình ảnh bằng cách sử dụng OpenCV?
- 7. làm thế nào để xoay một bitmap 90 độ
- 8. iOS CAKeyframeChế độ xoay vòngMode trên UIImageView
- 9. Làm cách nào để xoay hình ảnh?
- 10. Cách xoay nhãn trục x 90 độ ở mức độ
- 11. Xoay một mảng hai chiều 90 độ
- 12. Hình ảnh iOS quay trên nút bấm
- 13. CSS3 Xoay từ 0 đến 90 rồi 90 đến 180
- 14. Làm thế nào để kích hoạt một chức năng khi iPhone được xoay 90 độ?
- 15. Cách Xoay TextView 90 Độ và hiển thị
- 16. Làm cách nào để tạo hiệu ứng động/xoay UIView 90 degress từ góc trên bên phải của nó?
- 17. Làm thế nào để xoay một ma trận N x N 90 độ?
- 18. Làm cách nào để vẽ hình ảnh được xoay trên Canvas JavaFX?
- 19. Xoay 90 độ trong CSS trong IE8 và thấp hơn
- 20. xoay 3D trên hình ảnh
- 21. jquery xoay hình ảnh onclick
- 22. Khi thiết bị iOS xoay, làm cách nào để thay đổi hướng chỉ một phần tử?
- 23. Làm thế nào để cắt và xoay hình ảnh lập trình trong Android?
- 24. Làm thế nào để xoay hình ảnh bằng cách sử dụng jquery xoay plugin?
- 25. Tôi làm cách nào để định vị hình ảnh được xoay trong một vùng chứa?
- 26. Tăng tốc độ lưu hình ảnh - iOS
- 27. Xoay hình ảnh (s) OnClick với jQuery?
- 28. Cách xoay hình ảnh và lưu hình ảnh
- 29. 90 độ văn bản được xoay, tuôn ra từ trên cùng bên phải với CSS
- 30. Xoay hình ảnh kém
thể trùng lặp của [Định hướng hình ảnh kết quả của iOS UIImagePickerController sau khi tải lên] (http: // stackove rflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload) –
Tôi đã có cùng một vấn đề chính xác và tôi đã giải quyết nó bằng cách sử dụng danh mục được đề xuất trong phản hồi này: http://stackoverflow.com/a/5427890/1327557 Nó quản lý tất cả các trường hợp (phép quay và phép màu) –