2016-06-28 4 views
3

J'utilise UIImagePickerController pour capturer l'image en utilisant la caméra. L'orientation prise en charge pour mon application est Portrait. Je vois un comportement étrange pour l'iPhone 5. J'utilise Xcode 7 et swift 2.0. La version de l'iPhone 5 OS est 8.4. La cible de déploiement est de 8,0 pour mon application.Après avoir obtenu l'image de UIImagePickerController, UIImageView fait pivoter l'image pour l'iPhone 5

Les problèmes sont. 1. Pour l'iPhone 5, après la capture d'une image, l'image est affichée dans le mode respectif dans lequel l'image est capturée. Mais après que j'appuie sur l'option standard 'Use Photo' et quand l'image est affichée sur UIImageView, l'image est automatiquement tournée vers la gauche. Je ne sais pas pourquoi. Si je choisis l'image de la photothèque, l'image n'est pas pivotée. Je ne veux pas que l'image soit tournée. J'ai vu un message similaire avec une meilleure explication et une image réelle, mais il n'y a pas de réponse. UIImageView rotates image with retina 4 iPhone simulator but not retina 3.5/regular simulator J'ai essayé presque toute la solution rapide de ce poste: iOS UIImagePickerController result image orientation after upload et autre post aussi mais rien ne semble fonctionner. J'ai utilisé shouldAutorotate(), sFunc_imageFixOrientation(), et en ajoutant l'extension de ce post.

  1. De même, pour les deux appareils, après avoir appuyé sur l'option "Utiliser photo", il faut environ 10 secondes pour télécharger l'image. Peut-il être fait plus rapidement.

Voici mon code:

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

Répondre

2

C'est ce que j'ai observé. Lorsque vous capturez une image à l'aide de la caméra, l'image est pivotée de 90 degrés dans le sens inverse des aiguilles d'une montre et l'orientation de l'image est définie sur UIImageOrientation.Up. Le code de la fonction sFunc_imageFixOrientation iOS UIImagePickerController result image orientation after upload ne fonctionnera pas. Si vous sélectionnez une image dans la photothèque, l'image sera affichée telle quelle.

Fonction modifiée:

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 
    } 
} 

appeler cette fonction que si l'image est capturée à partir de l'appareil photo. Je sais que ma réponse ne sera pas parfaite mais vous pouvez certainement essayer si rien ne fonctionne pour vous.