我已经设置了一个UIView,其中将显示相机。一切正常,但是当我将AVCaptureVideoPreviewLayer的videoGravity设置为AVLayerVideoGravityResizeAspectFill时,似乎有些顶级&在点击的照片中也可以看到相机视图框外的底部部分。请参阅附图(https://i.stack.imgur.com/tDErR.jpg)
我尝试设置clipsToBounds = true但它似乎没有解决问题。
这是我的代码..
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var previewView: UIView!
@IBOutlet weak var capturedImage: UIImageView!
var session: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
override func viewDidLoad() {
super.viewDidLoad()
self.previewView.clipsToBounds = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configureCamera()
}
func configureCamera(){
session = AVCaptureSession()
session!.sessionPreset = AVCaptureSessionPresetPhoto
let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var error: NSError?
var input: AVCaptureDeviceInput!
do {
input = try AVCaptureDeviceInput(device: backCamera)
} catch let error1 as NSError {
error = error1
input = nil
print(error!.localizedDescription)
}
if error == nil && session!.canAddInput(input) {
session!.addInput(input)
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if session!.canAddOutput(stillImageOutput) {
session!.addOutput(stillImageOutput)
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
let rootLayer :CALayer = self.previewView.layer
rootLayer.masksToBounds = true
videoPreviewLayer?.frame = rootLayer.bounds
videoPreviewLayer?.masksToBounds = true
rootLayer.addSublayer((videoPreviewLayer)!)
rootLayer.borderColor = UIColor.green.cgColor
rootLayer.borderWidth = 2.0
self.previewView.clipsToBounds = true
session!.startRunning()
}
}
}
@IBAction func btnTakePhoto(_ sender: AnyObject) {
let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo)
if(videoConnection != nil) {
stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
if sampleBuffer != nil {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProvider(data: imageData as! CFData)
let cgImageRef = CGImage.init(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.relativeColorimetric)
let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)
self.capturedImage.image = image
self.session!.stopRunning()
}
})
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
videoPreviewLayer!.frame = self.previewView.layer.bounds
self.previewView.clipsToBounds = true
}
}
请注意,我不想使用任何其他视频游戏,如AVLayerVideoGravityResizeAspect.Please有人帮助我。我很挣扎。