在视图中包含 UIImagePickerController

Include UIImagePickerController in a view

我需要创建一个像页面一样工作的视图,并带有一个弹出窗口,我可以在其中选择从我的图库中挑选一张照片或创建一张新照片。

目前,我有一个 ViewController,显示为 Popover。在 ViewController 中,我插入了一个 ContainerView 并声明它的 class 是 UIImageViewController。这是向我展示照片库,但我找不到如何选择任何内容:My ViewController presented as Popover.

当我选择一张照片时,没有任何反应。我试过添加一些函数

 (func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!)) 

在我的 ViewController 中,但它不起作用。我读到 UIImagePickerController 不支持 subclassing,那么 Pages 是怎么做到的?

这是我的 ViewController 代码:

import UIKit

class MenuAddResources: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var newMedia: Bool?
    let imagePicker = UIImagePickerController()

    @IBAction func takePhoto(sender: AnyObject) {
            if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
            //load the camera interface
            let picker : UIImagePickerController = UIImagePickerController()
            picker.sourceType = UIImagePickerControllerSourceType.Camera
            picker.delegate = self
            picker.allowsEditing = false
            self.presentViewController(picker, animated: true, completion: nil)
            self.newMedia = true
        }
        else{
            //no camera available
            let alert = UIAlertController(title: NSLocalizedString("ERROR", comment: ""), message: NSLocalizedString("NO_CAMERA", comment: ""), preferredStyle: .Alert)
            alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .Default, handler: {(alertAction)in
            alert.dismissViewControllerAnimated(true, completion: nil)
        }))
            self.presentViewController(alert, animated: true, completion: nil)
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
        if error != nil {
            let alert = UIAlertController(title: NSLocalizedString("ERROR", comment: ""), message: NSLocalizedString("IMAGE_SAVE_FAILED", comment: ""), preferredStyle: UIAlertControllerStyle.Alert)

            let cancelAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .Cancel, handler: nil)

            alert.addAction(cancelAction)
            self.presentViewController(alert, animated: true, completion: nil)
        }
    }

    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
        self.dismissViewControllerAnimated(true, completion: { () -> Void in
        })

        // Let's store the image
        let now:Int = Int(NSDate().timeIntervalSince1970)
        let imageData = UIImageJPEGRepresentation(image, 85)
        //imageData?.writeToFile(documentsDirectory + "/\(now).jpg", atomically: true)
        print(imageData)

        /* will do stuff with the image */
    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

}

It appears you are using UIImagePickerViewController through a container view and therefore the delegate isnt set , thus no callbacks to receiving the image picked methods.

要解决此问题,您必须覆盖 class 中的 prepareForSegue 然后将 segue.destinationViewController 投射到您的选择器并将其 delegate 设置为 self