how to capture camera with UIImagePickerController in swift?

user3745888 picture user3745888 · Jun 17, 2014 · Viewed 44.8k times · Source

I'm trying use UIImagePickerController in swift but isn't work...

my ViewController:

class ViewController: UIViewController {

@IBOutlet var imag : UIView = nil
@IBAction func capture(sender : UIButton) {
    println("Button capture")
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
    {
        var imag = UIImagePickerController()
        imag.delegate = self
        imag.sourceType = UIImagePickerControllerSourceType.Camera;
       imag.mediaTypes = kUTTypeImage
        imag.allowsEditing = false

        self.presentViewController(imag, animated: true, completion: nil)

    }
   }   
}

I have errors in following line of code

imag.delegate = self 
(Type'ViewControlles does confoorm to protocol 'UIImagePickerControllerDelegate')
imagePicker.mediaTypes = kUTTypeImage   
(use of unresolved identifier kUTTypeImage)

I have read that kUTTypeImage cant use in swift.but don't know, i am using bad this functions. Any help?

Thanks!!

Answer

Filippo Camillo picture Filippo Camillo · Jun 20, 2014

You should also import MobileCoreServices in the controller:

import MobileCoreServices 

and then put the type inside square brackets like this:

image.mediaTypes = [kUTTypeImage]

Swift 2.0 and Higher

image.mediaTypes = [kUTTypeImage as String]