Reputation: 380
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
{
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
//imagePicker.mediaTypes = kUTTypeImage
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
I can't seem to convert to kUTTypeImage to swift, if I comment out this line the camera launches, but the screen is black. Any ideas? This is the objective C syntax:
imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
Upvotes: 2
Views: 6095
Reputation: 532
The code marked as the correct answer no longer works in Swift 4+ because some changes have been made.
UIImagePickerControllerSourceType.Camera
was changed to UIImagePickerController.SourceType.camera
and self.presentViewController(...)
to self.present(...)
Here is the same code that works for Swift 4+:
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera)
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
Upvotes: 0
Reputation: 1776
To add both movie and image, you could do this.
let availableMediaTypes = UIImagePickerController.availableMediaTypesForSourceType(.Camera)
self.imagePickercontroller.mediaTypes = availableMediaTypes!
You would notice you could either navigate to a video or capture image button when the imagePickercontroller has been presented. Following that just check the type of mediatype in the delegate method "didFinishPickingMediaWithInfo"
Upvotes: 0
Reputation: 11325
To define kUTTypeMovie
you could use:
imagePicker.mediaTypes = [String(kUTTypeMovie)]
Upvotes: 1
Reputation: 3338
I have tested those codes in XCode6 beta5, and found the following error:
fatal error: array element cannot be bridged to Objective-C
Thread 1:EXC_BREAKPOINT(code=EXC_ARM_BREAKPOINT, subcode-0xe7ffdefe)
The reason is that we can't assign an array with objects to imagePicker.mediaTypes like this in XCode6 beta5:
imagePicker.mediaTypes = [kUTTypeImage]
I initialized the array with object through the init function, and it works fine now↓↓↓
imagePicker.mediaTypes = NSArray(object: kUTTypeImage)
Upvotes: 2
Reputation: 380
Apparently it was this simple:
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
{
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.mediaTypes = [kUTTypeImage]
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
Upvotes: 3