如何在swift中使用UIImagePickerController捕获相机?

时间:2014-06-17 11:47:58

标签: uiimagepickercontroller swift ios8 xcode6

我正在尝试在swift中使用UIImagePickerController但是没有工作......

我的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)

    }
   }   
}

我在以下代码行中有错误

imag.delegate = self 
(Type'ViewControlles does confoorm to protocol 'UIImagePickerControllerDelegate')
imagePicker.mediaTypes = kUTTypeImage   
(use of unresolved identifier kUTTypeImage)

我已经读过kUTTypeImage无法在swift中使用。但是我不知道,我正在使用这个函数。有什么帮助吗?

谢谢!

6 个答案:

答案 0 :(得分:83)

您还应该在控制器中导入MobileCoreServices:

import MobileCoreServices 

然后将类型放在方括号内,如下所示:

image.mediaTypes = [kUTTypeImage]

Swift 2.0及更高

image.mediaTypes = [kUTTypeImage as String]

答案 1 :(得分:43)

Swift 2.0

在Swift 2.0(Xcode 7)中,您需要明确地将kUTTypeImageCFString)投射到String

picker.mediaTypes = [kUTTypeImage as String]

您仍然需要导入移动核心服务才能定义此符号:

import MobileCoreServices 

也就是说,mediaTypes的默认值无论如何都是[kUTTypeImage],所以如果你想要的话,你不需要设置它。

答案 2 :(得分:23)

您还应将UINavigationControllerDelegate添加到ViewController的协议列表中 和一个可选的委托功能(如果你计划得到一张照片)

这是您的问题的工作代码:

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)
        }
    }
}

答案 3 :(得分:7)

从你的代码中可以看出,你在两个地方犯错是设置delegate,第二个是设置媒体类型imag.mediaTypes = kUTTypeImage

第一个:如果您查看delegate的{​​{1}}定义,则需要确认两个协议UIImagePickerControllerUINavigationControllerDelegate,因此您必须在UIImagePickerControllerDelegate类就像

一样
viewcontroller

第二个错误:如果你查看class ViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate 的定义部分,它显然需要传递一系列媒体类型,所以这样做

mediaTypes

除此之外,我为同一个任务写了一个非常下降的课程

易于理解和整合。

Here you go

imag.mediaTypes = [kUTTypeImage]

答案 4 :(得分:1)

你必须像这样遵守代表

 class ViewController: UIViewController, UIImagePickerControllerDelegate

根据文档,默认情况下,媒体类型设置为图像,因此您可以继续删除该行,因为您只将其设置为图像。

不要忘记实施文档中概述的协议方法: documentation

答案 5 :(得分:0)

swift 1.2语法:

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



    }