Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何提高自定义相机视图质量+;代码_Ios_Iphone_Swift_View_Avfoundation - Fatal编程技术网

Ios 如何提高自定义相机视图质量+;代码

Ios 如何提高自定义相机视图质量+;代码,ios,iphone,swift,view,avfoundation,Ios,Iphone,Swift,View,Avfoundation,我学习了一个关于如何在youtube上创建自定义摄像头视图的快速教程,它工作正常……但由于某些原因,摄像头被放大了(不像iPhone上的普通摄像头&质量似乎被降级了,而且我不知道代码是否经过了非常优化,所以我想我应该问问像你们这样的在线专业人士:)如何提高\最大限度地提高我的相机质量和性能? 这是我的密码: 自定义摄影机视图代码: 它还没有拍照…但我真的很想在继续前进之前最大限度地提高我的相机性能和质量,我希望你能理解,谢谢你的帮助:) import UIKit import AVFoundat

我学习了一个关于如何在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()
        
    }
    
}