ios - 我的 uiimagePickerController didFinishPickingMediaWithInfo 永远不会被调用

标签 ios xcode swift camera

我尝试编写一个需要可以拍摄多张照片的屏幕的应用程序。我使用了来自 http://makeapppie.com/2015/11/04/how-to-make-xib-based-custom-uiimagepickercontroller-cameras-in-swift/ 的代码示例.

它似乎工作正常,但我的 imagePickerController didFinishPickingMediaWithInfo 更新被调用。我从 Xcode 收到一条错误消息“对尚未呈现的 View 进行快照会导致空快照。请确保您的 View 在快照之前至少呈现一次或在屏幕更新后进行快照。”在我看来这可能是问题所在,我已经用谷歌搜索了它,但还没有变得更聪明。很多人认为这是一个 Apple 错误,但我还没有找到任何人提供解决方案。

所以有人知道我的问题是否是 Xcode 错误吗,在那种情况下有解决方案还是我在代码中写错了什么:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, CustomOverlayDelegate {
    var picker = UIImagePickerController()

    @IBAction func shootPhoto(sender: AnyObject) {
        if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
            picker = UIImagePickerController() //make a clean controller
            picker.allowsEditing = false
            picker.sourceType = UIImagePickerControllerSourceType.Camera
            picker.cameraCaptureMode = .Photo
            picker.showsCameraControls = false

            //customView stuff
            let customViewController = CustomOverlayViewController(
                nibName:"CustomOverlayViewController",
                bundle: nil
            )
            let customView:CustomOverlayView = customViewController.view as! CustomOverlayView
            customView.frame = self.picker.view.frame
            customView.cameraLabel.text = "Hello Cute Camera"
            customView.delegate = self

            //presentation of the camera
            picker.modalPresentationStyle = .FullScreen
            presentViewController(picker, animated: true,completion: {
                self.picker.cameraOverlayView = customView
            })

        } else { //no camera found -- alert the user.
            let alertVC = UIAlertController(
                title: "No Camera",
                message: "Sorry, this device has no camera",
                preferredStyle: .Alert)
            let okAction = UIAlertAction(
                title: "OK",
                style:.Default,
                handler: nil)
            alertVC.addAction(okAction)
            presentViewController(
                alertVC,
                animated: true,
                completion: nil)
        }
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        print("didFinishPickingMediaWithInfo")
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //get the image from info
        UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil) //save to the photo library
    }

    //What to do if the image picker cancels.
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
    }

    //MARK: Custom View Delegates
    func didCancel(overlayView:CustomOverlayView) {
        picker.dismissViewControllerAnimated(true, completion: nil)
        print("dismissed!!")
    }
    func didShoot(overlayView:CustomOverlayView) {
        picker.takePicture()
        overlayView.cameraLabel.text = "Shot Photo"
        print("Shot Photo")
    }
    func weAreDone(overlayView: CustomOverlayView) {
        picker.dismissViewControllerAnimated(true,
            completion: nil)
        print("We are done!")
    }
}

最佳答案

picker.delegate = self 之后

picker = UIImagePickerController()

还继承了带有 UIImagePickerControllerDelegate 委托(delegate)的类。

它会起作用。

关于ios - 我的 uiimagePickerController didFinishPickingMediaWithInfo 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35866392/

相关文章:

ios - AVAudioEngine schedule sample 准确的参数变化

ios - 为 UISegmentedControl 定义点击事件

ios - 无法将 UIButton Outlet 添加到 UIView

ios - 使用 NSURL 的结果填充 UIView

iPhone 应用程序构建到模拟器,不会构建到设备

swift - 在 UIImageView 之上的 Swift 上将蒙版层作为 UIImage

编写惰性序列转换的 Swift 问题

ios - 将数据从一个 View Controller 传递到另一个 View Controller 而不进行转换

ios - Xcode错误: file was built for archive which is not the architecture being linked (x86_64)

ios - UITableViewCells 内的 UITextfield - 关闭键盘