我按照快速教程了解如何在youtube上创建自定义相机视图及其工作正常...但由于某种原因相机被放大(不像iPhone的普通相机和质量似乎降级,另外我不知道代码是否非常优化,所以我想我应该像在线一样在网上询问一些专业人士:) 我如何改进\最大化我的相机质量&性能吗
这是我的代码:
import UIKit
import AVFoundation
class CustomCameraViewController: UIViewController,AVCaptureVideoDataOutputSampleBufferDelegate,UIImagePickerControllerDelegate{
@IBOutlet var CameraView: UIView!
var audioPlayer = AVAudioPlayer()
let captureSession = AVCaptureSession()
var captureDevice: AVCaptureDevice?
var previewLayer : AVCaptureVideoPreviewLayer?
var frontCamera: Bool = false
var stilledImageOutput: AVCaptureStillImageOutput = AVCaptureStillImageOutput()
func beginSession(){
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.view.layer.addSublayer(previewLayer!)
previewLayer?.frame = self.view.layer.bounds
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
captureSession.startRunning()
stilledImageOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG ]
if captureSession.canAddOutput(stilledImageOutput){
captureSession.addOutput(stilledImageOutput)
}
}
func frontCamera(_ front: Bool){
let devices = AVCaptureDevice.devices()
do{
try captureSession.removeInput(AVCaptureDeviceInput(device:captureDevice))
}catch{
print("Error")
}
for device in devices!{
if((device as AnyObject).hasMediaType(AVMediaTypeVideo)){
if front{
if (device as AnyObject).position == AVCaptureDevicePosition.front {
captureDevice = device as? AVCaptureDevice
do{
try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
}catch{
}
break
}
}else{
if (device as AnyObject).position == AVCaptureDevicePosition.back {
captureDevice = device as? AVCaptureDevice
do{
try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
}catch{
}
break
}
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 10.0, *) {
let photoSettings = AVCapturePhotoSettings()
photoSettings.isHighResolutionPhotoEnabled = true
photoSettings.isAutoStillImageStabilizationEnabled = true
} else {
// Fallback on earlier versions.
}
frontCamera(frontCamera)
if captureDevice != nil{
beginSession()
let music = Bundle.main.path(forResource: "CameraShutterSFX", ofType: "mp3")
// copy this syntax, it tells the compiler what to do when action is received
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: music! ))
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try AVAudioSession.sharedInstance().setActive(true)
}
catch{
print("error playing sound")
}
}
}
func TakePhoto(_ sender: UIButton) {
audioPlayer.play()
audioPlayer.volume = 0.1
if #available(iOS 10.0, *) {
let photoSettings = AVCapturePhotoSettings()
photoSettings.isHighResolutionPhotoEnabled = true
photoSettings.isAutoStillImageStabilizationEnabled = true
} else {
// Fallback on earlier versions.
}
}
func ActivateFlash(_ sender: UIButton) {
let FlashValue = !sender.isSelected
sender.isSelected = FlashValue
if captureDevice!.hasTorch{
do{
try captureDevice!.lockForConfiguration()
captureDevice!.torchMode = captureDevice!.isTorchActive ? AVCaptureTorchMode.off : AVCaptureTorchMode.on
captureDevice!.unlockForConfiguration()
}catch{
}
}
}
func DismissAction(_ sender: UIButton) {
performSegue(withIdentifier: "Segue", sender: nil)
}
func SwitchCameraDirectionsButton(_ sender: Any) {
//Switch Camera to Front:
frontCamera = !frontCamera
captureSession.beginConfiguration()
let inputs = captureSession.inputs as! [AVCaptureInput]
for oldInput: AVCaptureInput in inputs{
captureSession.removeInput(oldInput)
}
frontCamera(frontCamera)
captureSession.commitConfiguration()
}
}
它还没有拍摄照片......但我真的很想在继续前进之前最大化我的相机性能和质量,我希望你理解,谢谢你的帮助:)