rFlex / SCRecorder

iOS camera engine with Vine-like tap to record, animatable filters, slow motion, segments editing
Apache License 2.0
3.06k stars 583 forks source link

is there any easy way to make a screen capture of the recorder? #203

Open tonychan818 opened 9 years ago

tonychan818 commented 9 years ago

I wanna use SCRecorder for both photo and video capture. but when switching between them, there is a 2 seconds delay. I wanna make a image to cover it. and I created this function for get the screen.

func captureVideoScreen(imageCapturedCB:((img:UIImage)->())){

    println("captureVideoScreen pagenum: \(self.currentPageNum)")

    var stillImageOutput = AVCaptureStillImageOutput()

    stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

    if let cs = self.recorder.captureSession{
        cs.addOutput(stillImageOutput)

        if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo){
            stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
                (sampleBuffer, error) in
                var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                var dataProvider = CGDataProviderCreateWithCFData(imageData)
                var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)
                var image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right)
                if let img = image{
                    imageCapturedCB(img: img)
                }

            })
        }
    }

}

but i got this error 2015-08-28 16:02:02.815 TCMedia[853:106239] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* Cannot add output <AVCaptureStillImageOutput: 0x1706241c0> to capture session <AVCaptureSession: 0x17020a590 [AVCaptureSessionPresetPhoto]> <AVCaptureDeviceInput: 0x17482d660 [Back Camera]> -> <AVCaptureVideoDataOutput: 0x17482d000> <AVCaptureDeviceInput: 0x17482d660 [Back Camera]> -> <AVCaptureStillImageOutput: 0x17482d3a0> <AVCaptureDeviceInput: 0x17482d660 [Back Camera]> -> <AVCaptureVideoPreviewLayer: 0x174426180> <AVCaptureDeviceInput: 0x17482dc00 [iPhone 咪高風]> -> <AVCaptureAudioDataOutput: 0x17482d240> because more than one output of the same type is unsupported.'

any idea?

twomedia commented 9 years ago

Try this:

[recorder capturePhoto:^(NSError *error, UIImage *image) {
        if (image) {
            [self showImageOverlayWithUIImage:image];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to capture photo" message:error.localizedDescription delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
}];

Hope that helps!! Just need to convert to Swift :)

twomedia commented 9 years ago

You should get this effect without the need to generate an image anyway since when you switch camera modes the last frame stays on screen until the mode is changed... Try this code when switching camera modes

#define kVideoPreset AVCaptureSessionPresetHigh
#define kPhotoPreset AVCaptureSessionPresetPhoto

if ([recorder.captureSessionPreset isEqualToString:kPhotoPreset]) {
        recorder.captureSessionPreset = kVideoPreset;
    } else {
        recorder.captureSessionPreset = kPhotoPreset;
    }
renjithn commented 9 years ago

I found that there would be some delay while switching from Video to Photo preset. You can put a UIVisualEffectView with blur effect to have a behaviour similar to the Native Camera App when it switches from video to photo mode.

rFlex commented 9 years ago

You can use -[SCRecorder snapshotOfLastVideoBuffer]

fayhot commented 9 years ago

may be you should snap the last video buffer when switch the device.