2012-07-26 41 views
32

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?

+0

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

+0

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

Trả lời

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

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

+0

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

+3

'Đô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 –

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

Không hoạt động với số độ tùy ý. –

+0

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

+0

Hoặc dễ dàng hơn, sử dụng UIGraphicsBeginImageContextWithOptions (rotatedSize, NO, image.scale) thay vì UIGraphicsBeginImageContext() – Borzh

8

Đơn giản chỉ cần thêm mã này vào hình ảnh.

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

Đâ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 UIImagerotationDegrees là một flote Ivar để xoay hình ảnh rằng các văn bằng

+1

mã này không hoạt động cho hình ảnh chế độ dọc cho lần đầu tiên. –

+1

Để 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

+0

@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 –

6

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)); 
0

Đâ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; 

} 
3

Swift 3 phiên bản:

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

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

Swift 4+.

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

Note: angle is in radian

.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:

enter image description here

Các vấn đề liên quan