2017-04-20 112 views
0

我正在掃描條形碼iOS應用使用AVFoundationAVfoundation相機熄滅屏幕的OBJ-C

所以我創建使用接口建設者約束方盒子。方盒與約束條件都很好。非常好。 我有以下代碼將avcapturelayer添加到方框中。

self.captureLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]; 
[self.captureLayer setFrame:self.cameraPreviewView.layer.bounds]; 
[self.captureLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 
[self.cameraPreviewView.layer addSublayer:self.captureLayer]; 

該圖層遵循方框約束的前導空間,但不跟蹤尾部。新添加的AVlayer會在屏幕上向右(右側),而方形盒本身都很好。我在這裏錯過了什麼?

謝謝!

回答

0

我認爲你應該嘗試設置self.captureLayer的邊界/位置,而不是幀? 乾杯!

0

如果您將框架設置爲viewDidLoad,可能會發生這種情況。如果是這樣,請嘗試使用viewWillAppear:代替。

0

這可能會解決你的問題

CGRect bounds=view.layer.bounds; 
captureLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
captureLayer.bounds=bounds; 
captureLayer.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); 

或者

因爲你使用的是AVLayerVideoGravityResizeAspectFill所以它會走出屏幕,你可以使用AVLayerVideoGravityResizeAspectFit代替。

您(使用,當你查看)

view.clipToBound=YES; 

比增加子層來查看

view.layer.masksToBounds = YES; 
需要設置 clipToBound=YES;使用 AVLayerVideoGravityResizeAspectFill
相關問題