samedi 1 août 2015

Lagging sound after taking a picture

Once you take a picture iPhone makes that shutter sound.

My problem is that after taking a picture I need to present new view controller once the picture is taken. But that sound doesn't end exactly when I have my picture data. So it ends up playing while segue from one view controller to another is being performed which makes it sound laggy.

I am looking for solution where I can still present my view controller but that sound is not laggy.

@IBAction func didPressCaptureButton(sender: AnyObject) {

    captureButton.hidden = true

    dispatch_async(self.sessionQueue!, {
        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) {
                    self.cameraDelegate?.cameraImageReady!(image: UIImage())
                    var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                    var dataProvider = CGDataProviderCreateWithCFData(imageData)
                    var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

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

                    /* If I do this then sound is not laggy. But I need to present another view controller, not just add subview.
                    var imageHolderView = UIView(frame: self.view.bounds)
                    var imageView = UIImageView(frame: imageHolderView.bounds)
                    imageView.image = UIImage(data: imageData)

                    imageHolderView.addSubview(imageView)
                    self.view.addSubview(imageHolderView)
                    */ 
                }
            })
        }
    })
  }

Aucun commentaire:

Enregistrer un commentaire