2016-06-28 84 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 
    } 
} 

呼叫只有当图像从相机拍摄此功能。我知道我的答案不会很完美,但如果没有任何事情可以为你解决,你绝对可以尝试。