gpt4 book ai didi

objective-c - 如何在 Mac 中使用 AVCaptureStillImageOutput 捕获屏幕

转载 作者:行者123 更新时间:2023-12-03 17:48:39 24 4
gpt4 key购买 nike

我想捕获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];
}];
}

最佳答案

听起来您想截屏。

这是我在 iOS 中用来拍摄整个 View 的图片 (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 获取包含任何连接的显示器内容的图像。还允许用户将图像保存到磁盘上的文件中。

关于objective-c - 如何在 Mac 中使用 AVCaptureStillImageOutput 捕获屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36647390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com