我有两个视图控制器:


CameraViewController
CameraPhotoDetailViewController


我先拍摄照片,然后对第二张照片进行拍照,然后将图像呈现给用户

我在CameraDelegate中创建了名为CameraViewController的协议:

@objc protocol CameraDelegate: class {
    optional func cameraImageReady(#image: UIImage)
    optional func didCloseCameraImageDetailView()
}


两个视图控制器都在实现此协议,并且都有委托

CameraViewController

class CameraViewController: UIViewController, AVCaptureFileOutputRecordingDelegate, CameraDelegate {

    // MARK:
    // MARK: - Property
    @IBOutlet weak var captureButton: UIButton!
    @IBOutlet var previewView: CameraPreviewView!

    weak var cameraDelegate: CameraDelegate?
    ...
 }


CameraPhotoDetailViewController

class CameraPhotoDetailViewController: UIViewController, UITextFieldDelegate, CameraDelegate {

    // MARK: Variables
    @IBOutlet weak var cameraDetailView: SpringView!
    @IBOutlet weak var cameraImageView: UIImageView!
    @IBOutlet weak var cameraImageNameLabel: UITextField!


    weak var cameraDelegate: CameraDelegate?
    // MARK: -
    ...
}


我在override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){}中的CameraViewController中分配给那些代表

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

        if let cameraPhotoDetailViewController = segue.destinationViewController as? CameraPhotoDetailViewController {
            println("Segue")
            self.cameraDelegate = cameraPhotoDetailViewController
            cameraPhotoDetailViewController.cameraDelegate = self
        }
    }


出于某种原因,当我从didCloseCameraImageDetailView()调用协议方法CameraPhotoDetailViewController时,代表可以工作,但是当我从cameraImageReady(#image: UIImage)调用协议方法CameraViewController时,代理不起作用。

CameraViewController呼叫

@IBAction func didPressCaptureButton(sender: AnyObject) {

        captureButton.hidden = true

        let videoOrientation =  (self.previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation

self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo).videoOrientation = videoOrientation

        // Flash set to Auto for Still Capture
        CameraViewController.setFlashMode(AVCaptureFlashMode.Auto, device: self.videoDeviceInput!.device)



if let videoConnection = self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
            videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait

            self.stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
                if (sampleBuffer != nil) {

                    var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                    var dataProvider = CGDataProviderCreateWithCFData(imageData)
                    var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

                    var image: UIImage = UIImage(data: imageData)!
                    self.cameraDelegate?.cameraImageReady!(image: image)
                 self.performSegueWithIdentifier(Constants.CAMERA_IMAGE_DETAIL_SEGUE, sender: self)
                }
            })
        }
}


CameraPhotoDetailViewController呼叫

@IBAction func didPressCloseButton(sender: AnyObject) {
    dismissViewControllerAnimated(false, completion: nil)
    cameraDelegate?.didCloseCameraImageDetailView!()
    //activityIndicator.removeFromSuperview()
}


我花了最后一个小时来弄清楚我所缺少的东西,但看不到它。我希望这里有人能看到它。

最佳答案

好的,我知道自己像个白痴-我先是调用委托方法,然后在prepareForSegue()中分配了委托...

关于ios - 代表停止工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31747748/

10-13 04:01