12522
StoryBoardで置いたUIViewと同じ位置・同じサイズでAVCaptureVideoPreviewLayer（カメラからの映像)を設置したいのですが、ズレが生じます。CaptureViewが同じにしたいUIViewです。どのように修正したらぴったりあいますか？ご存知の方、ご教授お願いします。

```

    // 画像を表示するレイヤーを生成.
    let myVideoLayer : AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer.layerWithSession(mySession) as! AVCaptureVideoPreviewLayer
    myVideoLayer.frame = self.CaptureView.frame        
    myVideoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    
    // Viewに追加.
    self.view.layer.addSublayer(myVideoLayer)
    
```
CC BY-SA 3.0
2015-07-21T05:33:20.410
12518
2015-08-08T18:55:20.290
2015-08-08T18:55:20.290
5519
8593
question
swift xcode
UIViewと同じ場所・同じサイズでAVCaptureVideoPreviewLayerを設置したい
```

    myVideoLayer.frame = self.CaptureView.bounds
    
```

ではありませんか？

* * *
```

    import UIKit
    import AVFoundation
    
    class ViewController: UIViewController {
    
      @IBOutlet weak var CaptureView: UIView!
      var myVideoLayer: AVCaptureVideoPreviewLayer!
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        let mySession = AVCaptureSession()
    
        self.myVideoLayer = AVCaptureVideoPreviewLayer.layerWithSession(mySession) as! AVCaptureVideoPreviewLayer
        self.myVideoLayer.frame = self.CaptureView.bounds
        self.myVideoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    
        self.CaptureView.layer.addSublayer(self.myVideoLayer)
      }
    
      override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        self.myVideoLayer.frame = self.CaptureView.bounds
      }
    }
    
```
CC BY-SA 3.0
2015-07-21T06:11:35.903
12522
2015-07-21T09:57:33.557
2015-07-21T09:57:33.557
10609
10609
12518
answer