37

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

Kamar Shad
  • 6,089
  • 1
  • 29
  • 56
user3745888
  • 6,143
  • 15
  • 48
  • 97
  • 1
    I found this: http://beefdev.blogspot.com/2012/02/use-of-undeclared-identifier.html Which basically states you need to link MobileCoreServices.framework to your binary, then import as a framework in the class you are using. This solved my problem. – Morkrom Dec 21 '14 at 21:40

7 Answers7

84

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]
Matthew Cawley
  • 2,828
  • 1
  • 31
  • 42
Filippo Camillo
  • 961
  • 5
  • 5
  • 3
    Thank you ! I just spent an hour trying to understand that "unresolved reference" compilation error ... !! – Sébastien Stormacq Sep 28 '14 at 16:40
  • Thanks, I couldn't get this working using a bridging header with `#import ` (to anyone else reading this: DON'T bother trying that) – ephemer Nov 20 '14 at 17:33
  • Note that kUTTypeImage is a CFString and mediaTypes is expect a string. For this you will want `imag.mediaTypes = [kUTTypeImage as String]` – Matthew Cawley Dec 04 '17 at 20:08
43

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.

Community
  • 1
  • 1
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • 1
    Isn't explicitly allowing only images a good practice in case the default value changes in the future? – Crashalot Mar 26 '16 at 06:56
24

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 UIKit
import MobileCoreServices

class 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)
        }
    }
}
Roman
  • 855
  • 1
  • 8
  • 15
  • Roman, can you please confirm this works for you with Beta3? I'm having trouble with the kUTTypeImage reference. thx – Narwhal Jul 15 '14 at 03:34
  • The delegate is different in beta 3 now. How to get UIImage in beta 3? – Bagusflyer Jul 16 '14 at 16:13
  • This is what I did: if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { } – elprl Aug 01 '14 at 16:21
  • 2
    In Beta5, imag.mediaTypes=[kUTTypeImage] would cause error. Change it to imagePicker.mediaTypes = NSArray(object: kUTTypeImage), and it works fine. – ohyes Aug 17 '14 at 03:48
  • You can find a modified version of this code which works in Beta 6 here: http://pastebin.com/5RxLDw0f – Apfelsaft Sep 11 '14 at 09:05
  • @ohyes you're right. Even in the Xcode 6.0.1 you need to type mediaTypes = NSArray(object: kUTTypeImage). Setting it as a normal swift array causes a crash. – Salman Hasrat Khan Sep 23 '14 at 12:10
  • You also have to add MobileCoreServices to Link Binaries with Libraries – Adrian Dec 08 '14 at 17:06
7

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 type imag.mediaTypes = kUTTypeImage

First One:If you look into the delegate definition of UIImagePickerController it requires to confirm two protocol UINavigationControllerDelegate and UIImagePickerControllerDelegate so you have to adopt these two protocols in your viewcontroller class like as

class ViewController: UIViewController,UINavigationControllerDelegate,    UIImagePickerControllerDelegate

second error:If you look into the definition part of mediaTypes it clearly requires array of media types to passed so do like this

imag.mediaTypes = [kUTTypeImage]

Apart from this, I have written a very descent class for the same task

It is easy to understand and integrate.

Here you go

//Declare property 
var imagePicker:ImageVideoPicker?

//Call below line of code properly, it will return an image

self.imagePicker = ImageVideoPicker(frame: self.view.frame, superVC: self) { (capturedImage) -> Void in
        if let captureImage = capturedImage{
        //you did it.....

        }

    }
Kamar Shad
  • 6,089
  • 1
  • 29
  • 56
  • Please can you give reason before down casting my answer.Please i would be very glad to know – Kamar Shad Sep 10 '14 at 12:38
  • I don't know why people are in so Hurry even they don't try to think,we are here to help each other.I request you all whenever you gonna make any down voting, Please do mention the reason in a comment.It will help me and others too – Kamar Shad Sep 10 '14 at 12:45
  • 2
    I think you should make it clearer that ImageVideoPicker is your own class. I didn't downvote you but I did waste time checking to see if ImageVideoPicker was a new swift-only class or something. – ephemer Nov 20 '14 at 17:31
  • 3
    You asked for an explanation and now you're belittling me for providing one. Classy. – ephemer Nov 24 '14 at 11:01
2

You have to conform to the delegate like this

 class ViewController: UIViewController, UIImagePickerControllerDelegate

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

Kris Gellci
  • 9,539
  • 6
  • 40
  • 47
  • but nnot in line imag.mediaTypes = [kUTTypeImage as String], exist kUTTypeImage in Swift?? or there are other form to use camera? – user3745888 Jun 17 '14 at 14:22
2

Try this

import UIKit
import AVFoundation

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var imagePicker: UIImagePickerController!

@IBOutlet weak var ImageView: UIImageView!



override func viewDidLoad() {
    super.viewDidLoad()


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func takeImage(sender: AnyObject) {

    imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .Camera
    presentViewController(imagePicker, animated: true, completion: nil)

}


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imagePicker.dismissViewControllerAnimated(true, completion: nil)
    ImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}

}
khan
  • 1,099
  • 1
  • 11
  • 21
0

swift 1.2 syntax:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        let image = info[UIImagePickerControllerOriginalImage]



    }