2

In my app, I have a button with which I open the Gallery as below:

@IBAction func selectPhoto() {
    let photoPicker = UIImagePickerController()
    photoPicker.delegate = self
    photoPicker.sourceType = .PhotoLibrary
    self.presentViewController(photoPicker, animated: true, completion: nil)
}

I let the user select a photo and save it:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    photoImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage        
    let fileName:String = "logo.png"        
    let arrayPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString        
    let pngFileName = arrayPaths.stringByAppendingPathComponent(fileName)                UIImagePNGRepresentation(photoImageView.image!)!.writeToFile(pngFileName, atomically:true)        
    imageSave.setObject(fileName, forKey: "pngFileName")        
    self.dismissViewControllerAnimated(false, completion: nil)
}

Then from View controller, the pictures saved is shown everytime the user gets to the scene using below code.

 override func viewDidLoad() {
    super.viewDidLoad()

    if imageSave.stringForKey("pngFileName") != nil
    {
        let path = NSSearchPathForDirectoriesInDomains(
            .DocumentDirectory, .UserDomainMask, true)[0] as NSString
        let fileName = NSUserDefaults.standardUserDefaults()
            .stringForKey("pngFileName")
        let imagePath = path.stringByAppendingPathComponent(fileName!)
        let image = UIImage(contentsOfFile: imagePath )            
        photoImageView.image = image

    }

Problem : When the image loads back onto the UIImageView, the orientation of the picture is sometimes rotated right by 90 degrees, sometimes rotated left by 90 degrees or sometimes shown as it was saved.

Please let me know what I am doing wrong here. Any help would be appreciated. Thanks

2
  • 1
    Possible duplicate of iPhone, how to detect orientation of image when it was taken Commented Jun 26, 2016 at 22:28
  • I tried to detect the Orientation of images that I am saving but then I am not able to apply those Orientation back to the image in viewDidLoad(). Any clue on how can the retrieved Orientation value be applied to the image in viewDidLoad() ? Thanks. Commented Jun 27, 2016 at 1:49

1 Answer 1

2

I had the same problem, because of different rect sizes where the photo is shown. You could do a trick and save instead of the original image a copied version. This will discard the rotation problem, which is actually a fitting issue.

let imageName = info[UIImagePickerControllerOriginalImage] as! UIImage
let thumb1 = UIImageJPEGRepresentation(imageName, 0.8)
btnImageView.image = UIImage(data: thumb1!)

This is just a quick and dirty answer, but worked for me. :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.