2016-04-15 51 views
0

Macの画面をキャプチャしたいのですが、AVCaptureStillImageOutputが動作していることがわかります。しかし、Macでの使用方法はわかりません。AVCaptureStillImageOutputを使用してMacで画面をキャプチャする方法

私は誰かに私が画面をキャプチャするためにクラスを使用することについていくつかのサンプルコードを与えることを願っています。またはいくつかのアドバイスはOKです。

次のコードは、AVCaptureStillImageOutputを使用してIOSデバイスの画像をキャプチャしています.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]; 
}]; 
    } 
+0

デスクトップアクティビティのムービーを作成することを意味する画面をキャプチャしますか? –

+0

画面の静止画像をキャプチャします。実際には、ビデオが録画されている間、デスクトップ活動の録画ビデオをマルチキャストグループに送信したいと思います。私はネットワークにストリームメディアを送信する方法を知らないので、ストリームメディアを送信する代わりに画像を連続して送信することは、私が考えることができる唯一の解決策です。 –

+0

あなたは1つを確認し、もう1つはやりたい私はあなたが何を望んでいるのか分からない。 –

答えて

1

画面キャプチャをしたいと思うようです。ここで

が、これはOSX上で動作する場合、私は知らない私はビュー全体(screenCapture)の写真を撮るためにiOSのに使用し、

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(); 

です。

編集 - [OK]を、上記の仕事、これはあなたを助けることができれば多分あなたが見ることができ、ここでScreenShot Apple Docs

はこれがないものについての説明ですいません - 内容を含む画像を取得するためにクォーツ・ディスプレイ・サービスを使用します接続されているディスプレイのまた、ユーザーはイメージをディスク上のファイルに保存することもできます。

+0

ありがとうございました。それは動作しませんが、 –

+0

あなたは何のエラーを受け取りましたか? – Ro4ch

+0

私は "UIGraphicsBeginImageContextWithOptions()"も見つけませんでした。これはiOSでの使用に過ぎません。 –

関連する問題