VideoPreviewLayerをiPhoneの完全な画面に埋め込む方法について、約100万本のスレッドを読みましたが、何も問題ありません。AVCaptureVideoPreviewLayerが画面に表示されない
これは私のプレビュー層のinitです:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Choosing bigger preset for bigger screen.
_sessionPreset = AVCaptureSessionPreset1280x720;
}
else
{
_sessionPreset = AVCaptureSessionPresetHigh;
}
[self setupAVCapture];
AVCaptureSession *captureSession = _session;
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
UIView *aView = self.view;
previewLayer.frame = aView.bounds;
previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
[aView.layer addSublayer:previewLayer];
私setupAvCapture方法です:
//-- Setup Capture Session.
_session = [[AVCaptureSession alloc] init];
[_session beginConfiguration];
//-- Set preset session size.
[_session setSessionPreset:_sessionPreset];
//-- Creata a video device and input from that Device. Add the input to the capture session.
AVCaptureDevice * videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if(videoDevice == nil)
assert(0);
//-- Add the device to the session.
NSError *error;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if(error)
assert(0);
[_session addInput:input];
//-- Create the output for the capture session.
AVCaptureVideoDataOutput * dataOutput = [[AVCaptureVideoDataOutput alloc] init];
[dataOutput setAlwaysDiscardsLateVideoFrames:YES]; // Probably want to set this to NO when recording
//-- Set to YUV420.
[dataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange]
forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; // Necessary for manual preview
// Set dispatch to be on the main thread so OpenGL can do things with the data
[dataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
[_session addOutput:dataOutput];
[_session commitConfiguration];
[_session startRunning];
私はすでに別のAVCaptureSessionPresetsとResizeFitオプションを使用しようとしました。しかし、それは常に次のようになります。
http://imageshack.us/photo/my-images/707/img0013g.png/
かを、この私が使用している場合previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill。レイヤーのサイズをログに記録すると、正しいフルスクリーンサイズが返されます。
http://imageshack.us/photo/my-images/194/img0014k.png/
previewLayer.frame = CGRectMake(0、0、1280、720)を設定した場合。手動でそれは働いている...誰もそれを説明することができます。幅/高さが反転しているようです(縦長)が、実際には横向きです。だから私はその実装を修正することができます_screenWidth = [UIScreen mainScreen] .bounds.size.width; _screenHeight = [UIScreen mainScreen] .bounds.size.height; if(_screenHeight> _screenWidth){ _screenWidth = _screenHeight; _screenHeight = [UIScreen mainScreen] .bounds.size.width; } – user2135074