【问题标题】:How to use the AVCaptureStillImageOutput to capture the screen in Mac如何使用 AVCaptureStillImageOutput 在 Mac 中捕获屏幕
【发布时间】:2016-04-15 12:38:52
【问题描述】:

我想截取 Mac 的屏幕,我知道 AVCaptureStillImageOutput 有效。但我不知道如何在 Mac 中使用它。

我希望有人能给我一些关于使用类捕获屏幕的示例代码。或者一些建议是可以的。

下面的代码是在IOS设备中使用AVCaptureStillImageOutput来抓图的。也许可以在Mac上修改使用?

任何帮助表示赞赏,在此先感谢。

/////////////////////////////////////////////////
////
//// Utility to find front camera
////
    /////////////////////////////////////////////////
    -(AVCaptureDevice *) frontFacingCameraIfAvailable{
    NSArray *videoDevices = [AVCaptureDevice
    devicesWithMediaType:AVMediaTypeVideo];
    AVCaptureDevice *captureDevice = nil;

    for (AVCaptureDevice *device in videoDevices){

      if (device.position == AVCaptureDevicePositionFront){

        captureDevice = device;
        break;
    }
}

//  couldn't find one on the front, so just get the default video device.
if (!captureDevice){

    captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}

return captureDevice;
}

/////////////////////////////////////////////////
////
//// Setup Session, attach Video Preview Layer
//// and Capture Device, start running session
////
/////////////////////////////////////////////////
-(void) setupCaptureSession {
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer    alloc] initWithSession:session];
[self.view.layer addSublayer:captureVideoPreviewLayer];

NSError *error = nil;
AVCaptureDevice *device = [self frontFacingCameraIfAvailable];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];

self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[self.stillImageOutput setOutputSettings:outputSettings];

[session addOutput:self.stillImageOutput];

[session startRunning];
}

/////////////////////////////////////////////////
////
//// Method to capture Still Image from 
//// Video Preview Layer
////
/////////////////////////////////////////////////
-(void) captureNow {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in self.stillImageOutput.connections) {
    for (AVCaptureInputPort *port in [connection inputPorts]) {
        if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) { break; }
}

NSLog(@"about to request a capture from: %@", self.stillImageOutput);
__weak typeof(self) weakSelf = self;
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *image = [[UIImage alloc] initWithData:imageData];

     [weakSelf displayImage:image];
 }];
    }

【问题讨论】:

  • 捕获屏幕意味着您正在谈论制作桌面活动的电影?
  • 捕获屏幕的静止图像。实际上,我想在录制视频时将桌面活动的录制视频发送到多播组。我不知道如何将流媒体发送到网络,所以我能想到的唯一解决方案是不断发送图像而不是发送流媒体。
  • 您确认了一个并想做另一个。我不知道你想要什么。
  • 只想捕捉屏幕的静止图像。
  • 查看 Apple 的名为 AVScreenShack 的示例项目。

标签: objective-c xcode macos cocoa


【解决方案1】:

听起来你想截屏。

这是我在 iOS 中用来拍摄整个视图 (screenCapture) 的照片,

UIView *wholeScreen = self.view;

// define the size and grab a UIImage from it
UIGraphicsBeginImageContextWithOptions(wholeScreen.bounds.size, wholeScreen.opaque, 0.0);
[wholeScreen.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screengrab = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我不知道这是否适用于 OSX。

编辑 - 好的,以上不起作用,也许你可以看看这是否对你有帮助,ScreenShot Apple Docs

以下是有关此功能的说明 - 使用 Quartz Display Services 获取包含任何已连接显示器内容的图像。还允许用户将图像保存到磁盘上的文件中。

【讨论】:

  • 我什至没有找到“UIGraphicsBeginImageContextWithOptions()”。它只是在iOS中使用。
  • 好的,我更新了我的答案,提供了指向 Apple Doc 的链接,其中包含有关如何捕获屏幕并保存到文件的代码。
  • 兄弟。我得到它。只需修改我发布的代码。无论如何感谢我的朋友。
  • 所以,你知道怎么做,但你想让我给你写出来??如果您想截屏,则不会使用您发布的方法。这就是我给你新代码的原因。
  • 对不起兄弟。我花了很多时间修改代码。当我工作时,我来这里告诉你。然后我看到你的更新答案......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
  • 2011-08-30
相关资源
最近更新 更多