Я пытаюсь захватить экран во время предварительного просмотра видео с помощью AVFoundation (AVCaptureDeviceInput и AVCaptureVideoDataOutput).
Запустить предварительный просмотр:
func startCamera(){
var screenSize = UIScreen.mainScreen().bounds.size;
self.previewView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height));
self.previewView.contentMode = UIViewContentMode.ScaleAspectFit
self.view.addSubview(previewView);
session.sessionPreset = AVCaptureSessionPresetHigh
let devices = AVCaptureDevice.devices();
// Loop through all the capture devices on this phone
for device in devices {
// Make sure this particular device supports video
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the front camera
if(device.position == AVCaptureDevicePosition.Back) {
captureDevice = device as? AVCaptureDevice;
if captureDevice != nil {
beginSession();
break;
}
}
}
}
}
func beginSession() {
var err : NSError? = nil
var deviceInput:AVCaptureDeviceInput = AVCaptureDeviceInput(device: captureDevice!, error: &err);
if err != nil {
println("error: \(err?.localizedDescription)");
}
if session.canAddInput(deviceInput){
session.addInput(deviceInput);
}
videoDataOutput = AVCaptureVideoDataOutput()
if let videoDataOutput = videoDataOutput {
var rgbOutputSettings = [NSNumber(integer: kCMPixelFormat_32BGRA):kCVPixelBufferPixelFormatTypeKey]
videoDataOutput.alwaysDiscardsLateVideoFrames=true;
videoDataOutput.setSampleBufferDelegate(self, queue:self.videoDataOutputQueue)
if session.canAddOutput(self.videoDataOutput){
session.addOutput(self.videoDataOutput)
}
videoDataOutput.connectionWithMediaType(AVMediaTypeVideo).enabled = true
if let previewLayer = AVCaptureVideoPreviewLayer(session: self.session) {
self.previewLayer = previewLayer
previewLayer.videoGravity = AVLayerVideoGravityResizeAspect
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
var rootLayer :CALayer = self.previewView.layer;
rootLayer.masksToBounds=true;
previewLayer.frame = rootLayer.bounds;
rootLayer.addSublayer(self.previewLayer);
session.startRunning();
delay(8, closure: { () -> () in
self.processImage()
})
}
}
}
Код для захвата экрана:
func processImage() {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
previewLayer!.renderInContext(UIGraphicsGetCurrentContext())
// tried previewView!.layer.render... to no avail
let previewImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
savePDFImage(previewImage, name: "front.pdf")
}
Возвращаемое изображение просто белое. Как я могу получить снимок экрана о том, что на экране при просмотре видео?