使用ImagePickerController拍摄视频/照片并在不同的视图上使用它们

时间:2015-04-18 03:14:18

标签: swift ios8 uiimagepickercontroller segue

有人可以指导我如何使用刚从另一个视图中的imagepicker拍摄的照片或视频。

说我按下相机按钮时从主视图中调用它。我想拍一张照片或视频然后再使用它。我想在另一个视图中执行此操作,我可以使用不同的自定义按钮,让我可以将它分享到Facebook,有什么用户以及回到我的主视图。我该怎么做呢?

这是我到目前为止的代码。

//
//  ViewController.swift
//  Cop Watch
//
//  Created by James Combs on 4/16/15.
//  Copyright (c) 2015 James's Source Code. All rights reserved.
//

import Foundation
import UIKit

class MainViewController: UIViewController, CameraViewControllerDelegate,  UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet internal var photosButton: UIBarButtonItem!
@IBOutlet internal var cameraButton: UIBarButtonItem!
@IBOutlet internal var goodBadInput: UITextField!
@IBOutlet internal var stoppedInput: UITextField!
@IBOutlet internal var ethnicityInput: UITextField!
@IBOutlet internal var nameInput: UITextField!

internal var imagePickerController: UIImagePickerController?
internal var camViewController: CameraViewController?
internal let camSourceError = "Camera is not available"

private var hasCancelledVideo: Bool = false
private var hasCancelledPhoto: Bool = false
private let cameraViewIdent = "CameraViewController"
private let photoViewIdent = "PhotoViewController"


    // Tried doing things here but unsure how to use this correctly for now with image picker
func cameraViewControllerDidFinish(controller: CameraViewController, imagePicker: UIImagePickerController) {

    /*imagePicker.delegate = self

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
    }

    imagePicker.allowsEditing = true
    controller.presentViewController(imagePicker, animated: true, completion: nil)
    controller.navigationController?.popViewControllerAnimated(true)*/
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

    self.dismissViewControllerAnimated(true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {

    self.dismissViewControllerAnimated(true, completion: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == self.cameraViewIdent {

            // Get a reference to the camera view controller - I THINK I COULD COMBINE THESE 2 IN ONE
        if let destinationVC = segue.destinationViewController as? CameraViewController {

        }
    }
    else if segue.identifier == self.photoViewIdent {

            // Get a reference to the photo view controller
        if let destinationVC = segue.destinationViewController as? PhotosViewController {


        }
    }
}

@IBAction func didPressPhotosButton(sender: AnyObject) {

}

@IBAction func didPressCameraButton(sender: AnyObject) {

    self.imagePickerController = UIImagePickerController()
    self.imagePickerController!.delegate = self

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
        self.imagePickerController!.sourceType = UIImagePickerControllerSourceType.Camera
    }

    var availableMediaTypes = UIImagePickerController.availableMediaTypesForSourceType(self.imagePickerController!.sourceType)

    self.imagePickerController!.mediaTypes = availableMediaTypes!
    self.imagePickerController!.allowsEditing = false
    self.presentViewController(self.imagePickerController!, animated: true, completion: nil)


}

override func viewDidLoad() {
    super.viewDidLoad()


}

override func viewDidAppear(animated: Bool) {

}


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


/*
    UITextField Delegates
*/

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    self.view.endEditing(true)
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {

    goodBadInput.resignFirstResponder()
    stoppedInput.resignFirstResponder()
    ethnicityInput.resignFirstResponder()
    nameInput.resignFirstResponder()

    return true
}


}

1 个答案:

答案 0 :(得分:1)

imagePickerController: didFinishPickingMediaWithInfo:

中使用此代码
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    imagePicker.dismissViewControllerAnimated(true, completion: nil)
    var photo = info[UIImagePickerControllerOriginalImage] as? UIImage
}

使用密钥UIImagePickerControllerMediaURL获取视频网址以捕获视频