2016-07-01 81 views
1

我有一個表格視圖,在單元格中有一個圖像和2個標籤。我正在使用imagePicker從我的相機膠捲中選擇一張照片並將該照片放入細胞圖像中。但是,當照片爲肖像模式照片時,圖像將旋轉90度。使用imagePicker時ImageView旋轉

在下面的代碼中,發佈是連接到表格視圖單元中的imageview的插座,並初始化了UIImagePickerController

class MakePostVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

@IBOutlet weak var postImg: UIImageView! 
@IBOutlet weak var postTitle: UITextField! 
@IBOutlet weak var postDescrip: UITextField! 

var imgPicker: UIImagePickerController! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    postImg.layer.cornerRadius = 30 
    postImg.clipsToBounds = true 

    imgPicker = UIImagePickerController() 
    imgPicker.delegate = self 

} 

這裏是我的功能didFinishPickingImage

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { 
    imgPicker.dismissViewControllerAnimated(true, completion: nil) 
    postImg.image = image 
} 

我在類似的帖子看着這裏SO但解決方案似乎並不管用。
有什麼建議嗎?

感謝

回答

2

UIImage由命名屬性imageOrientation。由於該屬性在Apple文檔中有所描述:

當繪製 時,圖像方向會影響圖像數據的顯示方式。默認情況下,圖像以「向上」的方向顯示。如果 圖像具有關聯的元數據(例如EXIF信息),但是,該屬性包含該元數據指示的方向。

所以這個屬性是用來反映相機相對於該ground.So定位,當你從手機攝像頭拍攝圖像,然後將其保存在UIImageimageOrientation財產方位標誌。
雖然顯示,如果此屬性被忽略,那麼你可能無法找到正確的方向image.Just供參考見下面的圖像如何攝像頭拍攝圖像,然後編碼和顯示。檢查link欲知更多細節。 enter image description here

要在IOS
Objective-C的

製作類

的UIImage + fixOrientation.h

@interface UIImage (fixOrientation) 

- (UIImage *)fixOrientation; 

@end 

的UIImage + fixOrientation解決這個問題。米

@implementation UIImage (fixOrientation) 

- (UIImage *)fixOrientation { 

    // No-op if the orientation is already correct 
    if (self.imageOrientation == UIImageOrientationUp) return self; 

    // We need to calculate the proper transformation to make the image upright. 
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. 
    CGAffineTransform transform = CGAffineTransformIdentity; 

    switch (self.imageOrientation) { 
     case UIImageOrientationDown: 
     case UIImageOrientationDownMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height); 
      transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

     case UIImageOrientationLeft: 
     case UIImageOrientationLeftMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.width, 0); 
      transform = CGAffineTransformRotate(transform, M_PI_2); 
      break; 

     case UIImageOrientationRight: 
     case UIImageOrientationRightMirrored: 
      transform = CGAffineTransformTranslate(transform, 0, self.size.height); 
      transform = CGAffineTransformRotate(transform, -M_PI_2); 
      break; 
     case UIImageOrientationUp: 
     case UIImageOrientationUpMirrored: 
      break; 
    } 

    switch (self.imageOrientation) { 
     case UIImageOrientationUpMirrored: 
     case UIImageOrientationDownMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.width, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
      break; 

     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRightMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.height, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
      break; 
     case UIImageOrientationUp: 
     case UIImageOrientationDown: 
     case UIImageOrientationLeft: 
     case UIImageOrientationRight: 
      break; 
    } 

    // Now we draw the underlying CGImage into a new context, applying the transform 
    // calculated above. 
    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, 
              CGImageGetBitsPerComponent(self.CGImage), 0, 
              CGImageGetColorSpace(self.CGImage), 
              CGImageGetBitmapInfo(self.CGImage)); 
    CGContextConcatCTM(ctx, transform); 
    switch (self.imageOrientation) { 
     case UIImageOrientationLeft: 
     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRight: 
     case UIImageOrientationRightMirrored: 
      // Grr... 
      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); 
      break; 

     default: 
      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage); 
      break; 
    } 

    // And now we just create a new UIImage from the drawing context 
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 
    UIImage *img = [UIImage imageWithCGImage:cgimg]; 
    CGContextRelease(ctx); 
    CGImageRelease(cgimg); 
    return img; 
} 

@end 

夫特

製作擴展作爲

extension UIImage { 

    func fixOrientation() -> UIImage { 

     // No-op if the orientation is already correct 
     if (self.imageOrientation == UIImageOrientation.Up) { 
      return self; 
     } 

     // We need to calculate the proper transformation to make the image upright. 
     // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. 
     var transform: CGAffineTransform = CGAffineTransformIdentity 

     if (self.imageOrientation == UIImageOrientation.Down || self.imageOrientation == UIImageOrientation.DownMirrored) { 
      transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height) 
      transform = CGAffineTransformRotate(transform, CGFloat(M_PI)) 
     } 

     if (self.imageOrientation == UIImageOrientation.Left || self.imageOrientation == UIImageOrientation.LeftMirrored) { 
      transform = CGAffineTransformTranslate(transform, self.size.width, 0) 
      transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2)) 
     } 

     if (self.imageOrientation == UIImageOrientation.Right || self.imageOrientation == UIImageOrientation.RightMirrored) { 
      transform = CGAffineTransformTranslate(transform, 0, self.size.height); 
      transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2)); 
     } 

     if (self.imageOrientation == UIImageOrientation.UpMirrored || self.imageOrientation == UIImageOrientation.DownMirrored) { 
      transform = CGAffineTransformTranslate(transform, self.size.width, 0) 
      transform = CGAffineTransformScale(transform, -1, 1) 
     } 

     if (self.imageOrientation == UIImageOrientation.LeftMirrored || self.imageOrientation == UIImageOrientation.RightMirrored) { 
      transform = CGAffineTransformTranslate(transform, self.size.height, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
     } 

     // Now we draw the underlying CGImage into a new context, applying the transform 
     // calculated above. 
     let ctx: CGContextRef = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height), 
                 CGImageGetBitsPerComponent(self.CGImage), 0, 
                 CGImageGetColorSpace(self.CGImage), 
                 CGImageGetBitmapInfo(self.CGImage).rawValue)!; 

     CGContextConcatCTM(ctx, transform) 

     if (self.imageOrientation == UIImageOrientation.Left || 
      self.imageOrientation == UIImageOrientation.LeftMirrored || 
      self.imageOrientation == UIImageOrientation.Right || 
      self.imageOrientation == UIImageOrientation.RightMirrored) { 
      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage) 
     } else { 
      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage) 
     } 

     // And now we just create a new UIImage from the drawing context and return it 
     return UIImage(CGImage: CGBitmapContextCreateImage(ctx)!) 
    } 
}