在视图中包含UIImagePickerController

Gui*_*rré 5 uiimagepickercontroller ios swift

我需要创建一个像页面一样的视图像页面一样工作,有一个弹出窗口,我可以选择从我的图书馆中选择一张照片或创建一个新照片.

目前,我有一个ViewController,呈现为Popover.在ViewController中,我插入了一个ContainerView,并声明它的类是UIImageViewController.这向我展示了照片库,但我找不到如何选择:我的ViewController呈现为Popover.

当我选择一张照片时,一切都没有发生.我试过放一些功能

 (func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!)) 
Run Code Online (Sandbox Code Playgroud)

在我的ViewController中,但它不起作用.我已经读过UIImagePickerController不支持子类化,所以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)
    }

}
Run Code Online (Sandbox Code Playgroud)

Shu*_*ank 2

看来您正在通过容器视图使用 UIImagePickerViewController ,因此未设置委托,因此没有回调来接收图像选取的方法。

要解决此问题,您必须在您的类中重写prepareForSegue,并将 投射segue.destinationViewController到您的选择器并将其设置delegateself