Captured image from camera is wrong orientation

Hi,


I have an application where the user can add items to core data and the items is shown in a tableview.

The items consist of a picture and description. When I get image from gallery the image is shown (in the image view) the correct orientation, but when I use the camera to capture a new image it shows the right orientation in the preview (shown directly on image view) but when I save the item and open the table view the picture is flipped in the wrong orientation. Regardless of the aspect ratio.


The image view is in Scale to fill mode, but I have tried several others.

This is the code for getting the picture:

    func getImage2(){
      
        self.alertController = UIAlertController(title: "Chose image source", message: "Do you want to use the camera or gallery?", preferredStyle: .ActionSheet)
      
        let cameraAction = UIAlertAction(title: "Camera", style: .Default) { (action) -> Void in
          
            if self.textInput.text != ""{
                if UIImagePickerController.isSourceTypeAvailable(
                    UIImagePickerControllerSourceType.Camera) {
                        let imagePicker = UIImagePickerController()
                      
                        imagePicker.delegate = self
                      
                        imagePicker.sourceType =
                            UIImagePickerControllerSourceType.Camera
                        imagePicker.mediaTypes = [kUTTypeImage as String]
                        imagePicker.allowsEditing = true
                      
                        self.presentViewController(imagePicker, animated: true,
                            completion: nil)
                }
                self.imgLabel.text = ""
            }
          
        }
      
        let galleryAction = UIAlertAction(title: "Chose from gallery", style: .Default) { (action) -> Void in
          
            if self.textInput.text != ""{
                if UIImagePickerController.isSourceTypeAvailable(
                    UIImagePickerControllerSourceType.SavedPhotosAlbum) {
                      
                        let imagePicker = UIImagePickerController()
                      
                        imagePicker.delegate = self
                        imagePicker.sourceType =
                            UIImagePickerControllerSourceType.PhotoLibrary
                        imagePicker.mediaTypes = [kUTTypeImage as String]
                        imagePicker.allowsEditing = true
                      
                        self.presentViewController(imagePicker, animated: true,
                            completion: nil)
                }
                self.imgLabel.text = ""
            }
          
        }
      
        alertController.addAction(cameraAction)
        alertController.addAction(galleryAction)
      
        self.presentViewController(self.alertController, animated: true, completion: nil)
      
    }


This is the tableview code:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       
        let cell:CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
       
        var itemWord : Words
       
        if(self.searchController.active){
       
            itemWord = self.filteredWords[indexPath.row]
           
       
        }else{
       
            itemWord = self.words[indexPath.row]
           
        }
      
       
        cell.textCell.text = itemWord.word
       
        if(itemWord.image != nil){
            cell.picText.hidden = true
            cell.imgCell.image = UIImage(data: itemWord.image!)
           
        }else{
       
            cell.picText.text = itemWord.word
           
        }
       
        return cell
    }


Do I have to do something special when using the camera instead of image gallery?

Hold it rotated by 180º!


Jan E.

I had the exact same problem. Images would show up rotated by 90 degrees. I changed saving the image to Core Data from .pngData to .jpegData and it was fixed.

Code Block
     newFoodItem.image = photoImage.jpegData(compressionQuality: 1.0)

Captured image from camera is wrong orientation
 
 
Q