dimanche 26 avril 2015

UIImagePickerController Rotating the Image in Swift


I am using UIImagePickerController to have the user of my app to take a photo or use the camera roll. When I tested the use camera option the image always rotates. But that never happens when I try to use the camera roll. This is the code I am using for the UIImagePickerController.

func photoLibary() {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.mediaTypes = [kUTTypeImage as NSString]
    imagePicker.allowsEditing = false

    self.view?.window?.rootViewController?.presentViewController(imagePicker, animated: true, completion: nil)
}

func camera() {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
    imagePicker.mediaTypes = [kUTTypeImage as NSString]
    imagePicker.allowsEditing = false

    self.view?.window?.rootViewController?.presentViewController(imagePicker, animated: true, completion: nil)
}

func choosePhoto() {

    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

    actionSheet.addAction(UIAlertAction(title: "Take Photo", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
        self.camera()
    }))

    actionSheet.addAction(UIAlertAction(title: "Camera Roll", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
        self.photoLibary()
    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

    self.self.view!.window!.rootViewController!.presentViewController(actionSheet, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    let image:UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    image.texture = SKTexture(image: image)
    picker.dismissViewControllerAnimated(true, completion: nil)

}


Aucun commentaire:

Enregistrer un commentaire