how to capture camera with UIImagePickerController in swift?
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]
Swift 2.0
In Swift 2.0 (Xcode 7), you need to explicitly cast kUTTypeImage
(a CFString
) to String
:
picker.mediaTypes = [kUTTypeImage as String]
And you still need to import Mobile Core Services for this symbol to be defined:
import MobileCoreServices
That said, the default value of mediaTypes
is [kUTTypeImage]
anyway, so you don't need to set it if that's what you want.
also you should add UINavigationControllerDelegate
to the protocols list of the ViewController
and one of the optional delegate functions (if you a planing to get a picture)
This is the working code for your issue:
import UIKitimport MobileCoreServicesclass ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate { func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){ println("i've got an image"); } @IBAction func capture(sender : UIButton) { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){ println("Button capture") var imag = UIImagePickerController() imag.delegate = self imag.sourceType = UIImagePickerControllerSourceType.Camera; imag.mediaTypes = [kUTTypeImage] imag.allowsEditing = false self.presentViewController(imag, animated: true, completion: nil) } }}