代表停止工作

时间:2015-07-31 13:45:40

标签: ios swift delegates

我有两个视图控制器:

  1. CameraViewController
  2. CameraPhotoDetailViewController
  3. 我在第一张照片然后执行第二张照片并将图像呈现给用户

    我在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()
    }
    

    我花了最后一小时试图弄清楚我错过了什么但却看不到它。我希望有人能看到它。

1 个答案:

答案 0 :(得分:0)

好的,我知道我觉得自己像白痴一样 - 我是第一次调用委托方法,然后我在prepareForSegue()分配了委托......