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!!
swift 1.2 syntax:
You should also import MobileCoreServices in the controller:
and then put the type inside square brackets like this:
Swift 2.0 and Higher
You have to conform to the delegate like this
Per documentation, by default the media types is set to image so you can go ahead and delete that line since you are only setting it to image.
Do not forget to implement the protocol methods which are outlined in the documentation: documentation
From Your Piece of code its very clear that you are making mistakes at two place one is setting
delegate
and second is setting Media typeimag.mediaTypes = kUTTypeImage
First One:If you look into the
delegate
definition ofUIImagePickerController
it requires to confirm two protocolUINavigationControllerDelegate
andUIImagePickerControllerDelegate
so you have to adopt these two protocols in yourviewcontroller
class like assecond error:If you look into the definition part of
mediaTypes
it clearly requires array of media types to passed so do like thisApart from this, I have written a very descent class for the same task
It is easy to understand and integrate.
Here you go
Try this
also you should add
UINavigationControllerDelegate
to the protocols list of theViewController
and one of the optional delegate functions (if you a planing to get a picture)This is the working code for your issue: