Tiz*_*ano 2 sdk avcapture swift ios8
我正在开发一个允许用户拍照的应用程序.我已经开始使用AVCam apple提供但我实际上有一个问题简单地说我无法将相机层定位在我想要的位置但是它自动定位在View的中心

在左侧,您可以看到我实际拥有的东西,右侧是我想拥有的东西.包含来自摄像头的预览的视图是UIView子类,这是代码
class AVPreviewView : UIView {
override class func layerClass() -> AnyClass {
return AVCaptureVideoPreviewLayer.self
}
func session () -> AVCaptureSession {
return (self.layer as AVCaptureVideoPreviewLayer).session
}
func setSession(session : AVCaptureSession) -> Void {
(self.layer as AVCaptureVideoPreviewLayer).session = session;
(self.layer as AVCaptureVideoPreviewLayer).videoGravity = AVLayerVideoGravityResizeAspect;
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏
首先获取屏幕大小,以便计算宽高比
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
var aspectRatio: CGFloat = 1.0
var viewFinderHeight: CGFloat = 0.0
var viewFinderWidth: CGFloat = 0.0
var viewFinderMarginLeft: CGFloat = 0.0
var viewFinderMarginTop: CGFlaot = 0.0
Run Code Online (Sandbox Code Playgroud)
现在计算预览图层的大小.
func setSession(session : AVCaptureSession) -> Void {
if screenWidth > screenHeight {
aspectRatio = screenHeight / screenWidth * aspectRatio
viewFinderWidth = self.bounds.width
viewFinderHeight = self.bounds.height * aspectRatio
viewFinderMarginTop *= aspectRatio
} else {
aspectRatio = screenWidth / screenHeight
viewFinderWidth = self.bounds.width * aspectRatio
viewFinderHeight = self.bounds.height
viewFinderMarginLeft *= aspectRatio
}
(self.layer as AVCaptureVideoPreviewLayer).session = session;
Run Code Online (Sandbox Code Playgroud)
将图层设置为videoGravity,AVLayerVideoGravityResizeAspectFill以便在给定自定义视图的情况下图层会拉伸以填充.
(self.layer as AVCaptureVideoPreviewLayer).videoGravity = AVLayerVideoGravityResizeAspectFill;
Run Code Online (Sandbox Code Playgroud)
最后,将预览图层的框架设置为上面计算的值,使用您喜欢的任何偏移量.
(self.layer as AVCaptureVideoPreviewLayer).frame = CGRectMake(viewFinderMarginLeft, viewFinderMarginTop, viewFinderWidth, viewFinderHeight)
}
Run Code Online (Sandbox Code Playgroud)
这可能需要一些调整,因为我还没有测试它,但你应该能够创建一个更灵活的VideoPreviewArea由APPreviewView的边界分隔.
| 归档时间: |
|
| 查看次数: |
7973 次 |
| 最近记录: |