how to capture camera with UIImagePickerController

2019-01-31 18:25发布

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

7条回答
smile是对你的礼貌
2楼-- · 2019-01-31 18:47

swift 1.2 syntax:

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



    }
查看更多
Ridiculous、
3楼-- · 2019-01-31 18:54

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]
查看更多
迷人小祖宗
4楼-- · 2019-01-31 18:58

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

查看更多
男人必须洒脱
5楼-- · 2019-01-31 19:01

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

        }

    }
查看更多
▲ chillily
6楼-- · 2019-01-31 19:02

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
}

}
查看更多
女痞
7楼-- · 2019-01-31 19:07

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)
        }
    }
}
查看更多
登录 后发表回答