2016-06-28 142 views
3

我使用UIImagePickerController捕捉圖像使用相機。我的應用支持的方向是肖像。我看到iPhone 5的奇怪行爲。我正在使用Xcode 7和Swift 2.0。 iPhone 5操作系統版本是8.4。我的應用的部署目標爲8.0。從UIImagePickerController獲取圖像後,UIImageView旋轉圖像iPhone 5

問題是。 1.對於iPhone 5,拍攝圖像後,圖像以拍攝圖像的相應模式顯示。但是在按下'使用照片'標準選項後,當圖像顯示在UIImageView上時,圖像會自動旋轉到左側。不知道爲什麼。如果我從照片庫中選擇圖像,圖像不會旋轉。我不想讓圖像旋轉。 我看到類似的帖子,更好的解釋和實際的形象,但沒有回答。 UIImageView rotates image with retina 4 iPhone simulator but not retina 3.5/regular simulator 我嘗試了幾乎所有這個帖子的快速解決方案:iOS UIImagePickerController result image orientation after upload和其他帖子,但似乎沒有任何工作。我使用了shouldAutorotate(),sFunc_imageFixOrientation(),並添加了這篇文章的擴展。

  1. 此外,對於這兩種設備,按'使用照片'選項後,上傳圖像大約需要10秒。它可以做得更快。

這裏是我的代碼:

FUNC openCamera(){

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) { 
     dispatch_async(dispatch_get_main_queue(), { 
      let imagePicker = UIImagePickerController(); 
      imagePicker.sourceType = UIImagePickerControllerSourceType.Camera; 
      imagePicker.allowsEditing = false; 
      imagePicker.delegate = self; 
      imagePicker.modalPresentationStyle = .FormSheet 
      self.presentViewController(imagePicker, animated: true, completion: nil); 
     }); 

    } 
} 

func openGallary() { 
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) { 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary; 
     imagePicker.allowsEditing = true 
     self.presentViewController(imagePicker, animated: true, completion: nil) 
    } 
} 

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 
    profileImage?.image = image 
    self.dismissViewControllerAnimated(true, completion: nil); 
} 

回答

2

這是我觀察到的東西。使用相機捕捉圖像時,圖像逆時針旋轉90度,圖像方向設置爲UIImageOrientation.Up,因此函數sFunc_imageFixOrientation iOS UIImagePickerController result image orientation after upload中的代碼將不起作用。如果您從照片庫中選擇圖像,則圖像將按照原樣顯示。

修改功能:

func sFunc_imageFixOrientation(img:UIImage) -> UIImage { 

     // 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 
     // Rotate image clockwise by 90 degree 
     if (img.imageOrientation == UIImageOrientation.Up) { 
      transform = CGAffineTransformTranslate(transform, 0, img.size.height); 
      transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2)); 
     } 

     if (img.imageOrientation == UIImageOrientation.Down 
      || img.imageOrientation == UIImageOrientation.DownMirrored) { 

      transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height) 
      transform = CGAffineTransformRotate(transform, CGFloat(M_PI)) 
     } 

     if (img.imageOrientation == UIImageOrientation.Left 
      || img.imageOrientation == UIImageOrientation.LeftMirrored) { 

      transform = CGAffineTransformTranslate(transform, img.size.width, 0) 
      transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2)) 
     } 

     if (img.imageOrientation == UIImageOrientation.Right 
      || img.imageOrientation == UIImageOrientation.RightMirrored) { 

      transform = CGAffineTransformTranslate(transform, 0, img.size.height); 
      transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2)); 
     } 

     if (img.imageOrientation == UIImageOrientation.UpMirrored 
      || img.imageOrientation == UIImageOrientation.DownMirrored) { 

      transform = CGAffineTransformTranslate(transform, img.size.width, 0) 
      transform = CGAffineTransformScale(transform, -1, 1) 
     } 

     if (img.imageOrientation == UIImageOrientation.LeftMirrored 
      || img.imageOrientation == UIImageOrientation.RightMirrored) { 

      transform = CGAffineTransformTranslate(transform, img.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(img.size.width), Int(img.size.height), 
                CGImageGetBitsPerComponent(img.CGImage), 0, 
                CGImageGetColorSpace(img.CGImage), 
                CGImageGetBitmapInfo(img.CGImage).rawValue)!; 
     CGContextConcatCTM(ctx, transform) 


     if (img.imageOrientation == UIImageOrientation.Left 
      || img.imageOrientation == UIImageOrientation.LeftMirrored 
      || img.imageOrientation == UIImageOrientation.Right 
      || img.imageOrientation == UIImageOrientation.RightMirrored 
      || img.imageOrientation == UIImageOrientation.Up 
      ) { 

      CGContextDrawImage(ctx, CGRectMake(0,0,img.size.height,img.size.width), img.CGImage) 
     } else { 
      CGContextDrawImage(ctx, CGRectMake(0,0,img.size.width,img.size.height), img.CGImage) 
     } 


     // And now we just create a new UIImage from the drawing context 
     let cgimg:CGImageRef = CGBitmapContextCreateImage(ctx)! 
     let imgEnd:UIImage = UIImage(CGImage: cgimg) 

     return imgEnd 
    } 
} 

呼叫只有當圖像從相機拍攝此功能。我知道我的答案不會很完美,但如果沒有任何事情可以爲你解決,你絕對可以嘗試。