没有UIImagePickerController用我自己的按钮拍照

时间:2015-02-14 10:16:24

标签: ios xcode swift camera uiimagepickercontroller

我想在我的应用中使用自己的按钮拍摄照片而不使用UIImagePickerController。所以,我希望在我的视图控制器上显示一个视图,该视图显示实时摄像机视图和一个带有动作的按钮,我可以在不使用UIImagePickerController的情况下直接拍摄照片。然后我想在我的视图控制器的视图中显示这张照片。

原因是我不希望第二个屏幕拍摄照片。处理应该很容易。或者是否有另一种解决方案可以在我自己的手柄上拍照?

我使用Swift来做到这一点。

目前我在代码中使用UIImagePickerController

import UIKit

class ViewController2:UIViewController,UIAlertViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate
{
    @IBOutlet weak var btnClickMe: UIButton!
    @IBOutlet weak var imageView: UIImageView!
    var picker:UIImagePickerController?=UIImagePickerController()
    var popover:UIPopoverController?=nil

    override func viewDidLoad()
    {
        super.viewDidLoad()
        picker!.delegate=self
    }

    override func didReceiveMemoryWarning() 
    {
        super.didReceiveMemoryWarning()
    }

    @IBAction func btnImagePickerClicked(sender: AnyObject)
    {
        self.openCamera()
    }

    func openCamera()
    {
        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
        {
            picker!.sourceType = UIImagePickerControllerSourceType.Camera
            self.presentViewController(picker!, animated: true, completion: nil)
        }
        else
        {
        openGallary()
        }
    }

    func openGallary()
    {
        picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone
        {
            self.presentViewController(picker!, animated: true, completion: nil)
        }
        else
        {
            popover=UIPopoverController(contentViewController: picker!)
            popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
        }
    }

    func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]!)
    {
        picker.dismissViewControllerAnimated(true, completion: nil)
        imageView.image=info[UIImagePickerControllerOriginalImage] as UIImage
    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController!)
    {
        println("picker cancel.")
    }
}

1 个答案:

答案 0 :(得分:0)