2017-02-20 95 views
0
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {} 

從上面的代碼中檢索到的圖像將旋轉圖像90度任何解決方案?Ios快速攝像頭圖像選擇器旋轉問題

+0

這不是Swift 3語法。是的,這不是問題,但請發佈更多的代碼 - 我沒有在任何時間遇到過這個問題。 – dfd

+0

已嘗試用照相機不照片libarary? – Jayu

回答

0

您用於獲取圖像的方法,即didFinishPickingImagedepreciated。使用以下代理功能獲取圖像:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

} 
+0

不能正常工作,我試着用你的建議 – Jayu

0

我有同樣的問題。請檢查這個UIImage擴展:

import UIKit 

extension UIImage { 

    func fixedOrientation() -> UIImage { 
     // No-op if the orientation is already correct 
     if (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 = CGAffineTransform.identity 

     if (imageOrientation == UIImageOrientation.down 
      || imageOrientation == UIImageOrientation.downMirrored) { 

      transform = transform.translatedBy(x: size.width, y: size.height) 
      transform = transform.rotated(by: CGFloat(M_PI)) 
     } 

     if (imageOrientation == UIImageOrientation.left 
      || imageOrientation == UIImageOrientation.leftMirrored) { 

      transform = transform.translatedBy(x: size.width, y: 0) 
      transform = transform.rotated(by: CGFloat(M_PI_2)) 
     } 

     if (imageOrientation == UIImageOrientation.right 
      || imageOrientation == UIImageOrientation.rightMirrored) { 

      transform = transform.translatedBy(x: 0, y: size.height); 
      transform = transform.rotated(by: CGFloat(-M_PI_2)); 
     } 

     if (imageOrientation == UIImageOrientation.upMirrored 
      || imageOrientation == UIImageOrientation.downMirrored) { 

      transform = transform.translatedBy(x: size.width, y: 0) 
      transform = transform.scaledBy(x: -1, y: 1) 
     } 

     if (imageOrientation == UIImageOrientation.leftMirrored 
      || imageOrientation == UIImageOrientation.rightMirrored) { 

      transform = transform.translatedBy(x: size.height, y: 0); 
      transform = transform.scaledBy(x: -1, y: 1); 
     } 


     // Now we draw the underlying CGImage into a new context, applying the transform 
     // calculated above. 
     let ctx:CGContext = CGContext(data: nil, width: Int(size.width), height: Int(size.height), 
             bitsPerComponent: cgImage!.bitsPerComponent, bytesPerRow: 0, 
             space: cgImage!.colorSpace!, 
             bitmapInfo: cgImage!.bitmapInfo.rawValue)! 

     ctx.concatenate(transform) 


     if (imageOrientation == UIImageOrientation.left 
      || imageOrientation == UIImageOrientation.leftMirrored 
      || imageOrientation == UIImageOrientation.right 
      || imageOrientation == UIImageOrientation.rightMirrored 
      ) { 


      ctx.draw(cgImage!, in: CGRect(x:0,y:0,width:size.height,height:size.width)) 

     } else { 
      ctx.draw(cgImage!, in: CGRect(x:0,y:0,width:size.width,height:size.height)) 
     } 


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

     return imgEnd 
    } 
} 

讓我知道它是否適合你。 :)

+0

謝謝@Kamil,但我不需要任何處理圖像 – Jayu

+0

在'func imagePickerController(_ picker:UIImagePickerController,didFinishPickingImage image:UIImage,editingInfo:[String:AnyObject]?){} '方法調用'let fixedImage = image.fixedOrientation' 固定圖像將不旋轉90度。你也可以查看這個擴展:http://stackoverflow.com/a/15850644/4219574 –