Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to capture camera with UIImagePickerController in swift?

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!!

like image 613
user3745888 Avatar asked Jun 17 '14 11:06

user3745888


People also ask

What is kUTTypeImage?

kUTTypeImage is actually default for the mediaTypes property. It states, that one can only pick still images. If you are ok with this default, you don't need to set it explicitly in your code.


Video Answer


2 Answers

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] 
like image 67
Filippo Camillo Avatar answered Sep 23 '22 12:09

Filippo Camillo


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.

like image 43
Aaron Brager Avatar answered Sep 21 '22 12:09

Aaron Brager