2015-05-19 31 views
4

我正在尋找一個程序化解決方案來解決以下問題:我想在相機(iOS)上繪製自定義疊加層。我希望它在攝像機輸出視圖中垂直居中。我已經完成了將我的自定義視圖相對於屏幕繪製爲中心,但不是相機圖片。iOS - 在相機上定位自定義覆蓋圖(垂直對齊)。頂部黑色條的大小

要做到這一點,我需要得到大小的頂部黑色條。我怎麼才能得到它?

頂部和底部酒吧的大小不相等,這就是爲什麼我得到的圖片有這個令人討厭的y偏移到底部。

Before shooting a pic

通知所產生的PIC的偏移: After shooting a pic

+0

設置AVCaptureSession並附加AVCaptureVideoPreviewLayer,然後您將完全控制攝像頭視圖的大小和位置。 – Sten

+0

問題是,我只有攝像頭視圖,沒關係,但我也想擁有'UIImagePickerController'提供的所有功能。我只想知道**在哪裏**這個視圖控制器放置其相機視圖 –

+0

AVCaptureSession擁有UIImagePickerController提供的一切以及更多。 UIImagePickerController旨在用作添加標準攝像機的簡單方法,而不是用於詳細控制。意志的佈局可能在不同的硬件上看起來不同。 – Sten

回答

1

好球員,我結束了使用AVFoundation,這到底是更好的解決方案,因爲它是定製的。我有self.fullScreenImageViewself.previewLayer作爲類屬性。你需要編寫自己的代碼來實現按鈕和攝影鏡頭的代碼。在這裏你可以玩照片的質量來獲得最佳的尺寸/質量。我用0.6,但你可以選擇任何你想要的。

@import MobileCoreServices; 
@import AVFoundation; 

    - (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    [self.view setBackgroundColor:[UIColor blackColor]]; 

    self.fullScreenImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
    self.fullScreenImageView.contentMode = UIViewContentModeScaleAspectFit; 
    [self.fullScreenImageView setCenter:self.view.center]; 

     AVCaptureSession *captureSession = [[AVCaptureSession alloc] init]; 
     captureSession.sessionPreset = AVCaptureSessionPresetHigh; 
     AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
     if (captureDevice) { 
      NSError *error; 
      AVCaptureDeviceInput *captureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error]; 
      if (!error && [captureSession canAddInput:captureDeviceInput]) { 
       [captureSession addInput:captureDeviceInput]; 
      } 
     } 

     AVCaptureStillImageOutput *stillImageOutput = [AVCaptureStillImageOutput new]; 
     [stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG, 
               AVVideoQualityKey : @(0.6)}]; 
     [captureSession addOutput:stillImageOutput]; 

     self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession]; 
     [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 
     self.previewLayer.frame = self.fullScreenImageView.bounds; 
     self.previewLayer.position = CGPointMake(CGRectGetMidX(self.fullScreenImageView.bounds), CGRectGetMidY(self.fullScreenImageView.bounds)); 
     [self.fullScreenImageView.layer addSublayer:self.previewLayer]; 
     [captureSession startRunning]; 

     CGRect circleRect = CGRectMake(0, (self.fullScreenImageView.bounds.size.height - self.fullScreenImageView.bounds.size.width)/2, self.fullScreenImageView.bounds.size.width, self.fullScreenImageView.bounds.size.width); 
     UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:circleRect]; 
     CAShapeLayer *ringLayer = [CAShapeLayer layer]; 
     ringLayer.path = circle.CGPath; 
     ringLayer.fillColor = nil; 
     ringLayer.strokeColor = [UIColor redColor].CGColor; 
     ringLayer.lineWidth = 2.0; 
     ringLayer.lineDashPattern = @[@5.0, @10.0]; 
     [self.fullScreenImageView.layer addSublayer:ringLayer]; 

     [self.navigationController setToolbarHidden:NO]; 
     [self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque]; 
     [self.navigationController.toolbar setTintColor:[UIColor whiteColor]]; 
     // Add here some buttons, which are standard UIBarButtonItems 

    [self.view addSubview:self.fullScreenImageView]; 
}