Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

how to handle image orientation

i have an issue on capture image from camera i have an application which i capture the image from camera using UIImagePickerController and after Capturing i save the image in my document directory and display it on my custom collection view but the problem is when i capture image and save it to document directory and when i load the image in my collection view it display for the user on wrong orientation how can i handle the image orientation i try to save image on JPEG its work good but it support only one orientation i want save image on png because it support all orientation any help please

the following my code:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { let image = info[UIImagePickerControllerOriginalImage] as! UIImage

// handle image orientation 

//end

// code for save image in document directory  from camera roll
let fileManager = NSFileManager.defaultManager()
do {
    let document = try fileManager.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
    let getFolders = try fileManager.contentsOfDirectoryAtURL(document, includingPropertiesForKeys: nil, options: .SkipsHiddenFiles)
    var maxImageNumber:Int = 0 // for get the max number image in document directory
    for folder in getFolders {
        if folder.lastPathComponent! == albumName {
            let getImagesCheck = try fileManager.contentsOfDirectoryAtURL(folder, includingPropertiesForKeys: nil, options: .SkipsHiddenFiles)
            let getImages = try fileManager.contentsOfDirectoryAtURL(folder, includingPropertiesForKeys: nil, options: .SkipsHiddenFiles)

            if getImages.count <= 0 {
                let imageUrl = folder.URLByAppendingPathComponent("Image \(getImagesCheck.count + 1).png")
                if let convertImage = UIImagePNGRepresentation(image) {
                    convertImage.writeToURL(imageUrl, atomically: true)
                }
            }else {
                // continue save images
                let getImages_else = try fileManager.contentsOfDirectoryAtURL(folder, includingPropertiesForKeys: nil, options: .SkipsHiddenFiles)

                for img in getImages_else {
                    let getImageName = img.lastPathComponent!
                    let arrayOne = getImageName.componentsSeparatedByString(".")
                    let arrayTwo = arrayOne[0].componentsSeparatedByString(" ")
                    let getImageNumber = Int(arrayTwo[1])
                    if getImageNumber! > maxImageNumber {
                        maxImageNumber = getImageNumber!
                    }
                }

                let imageUrl = folder.URLByAppendingPathComponent("Image \(maxImageNumber + 1).png")
                if let convertImage = UIImagePNGRepresentation(image) {
                    convertImage.writeToURL(imageUrl, atomically: true)
                }

            }
        }
    }

}catch {
    print(error)
}

self.dismissViewControllerAnimated(true, completion: nil)

}