微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 AvcaputrePhotoOutout 捕获照片以放置在 imageview 上

如何解决从 AvcaputrePhotoOutout 捕获照片以放置在 imageview 上

我希望我的 swift 代码拍摄一张照片并将其放在 imageview pic 上。你可以看到我在 func take Photo 中做了什么。该代码调用时导致运行时错误我不知道在代码中放置什么以使其不会导致运行时错误并拍摄照片并将其放置在 imageview pic 上。我想我在下面有大部分需要的代码

import UIKit;import AVFoundation

class ViewController: UIViewController {

    var b1 = UIButton()
    var pic = UIImageView()
    var caputreSession = AVCaptureSession()
    var backCamera : AVCaptureDevice?
    var frontCamera : AVCaptureDevice?
    var currentCamera: AVCaptureDevice?
    var photoOutput : AVCapturePhotoOutput?
    var image : UIImage?
    var captureSESSION = AVCaptureSession()
    
    var cameraPreviewLayer : AVCaptureVideoPreviewLayer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        
        setupCaputerSession()
        setUpDevice()
        setupinput()
        setUpPreivewLayer()
        startRunningCaptiureSessions()
        
        
        
        [b1,pic].forEach{
            
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
            
        }
        pic.backgroundColor = .purple
        b1.backgroundColor = .orange
        b1.frame = CGRect(x: 100,y: 100,width: 100,height: 100)
        b1.addTarget(self,action: #selector(takePHoto),for: .touchDown)
        cameraPreviewLayer?.frame = CGRect(x: 100,y: 300,width: 200,height: 200)
        
        pic.frame = CGRect(x: 100,y: 500,height: 100)
    }
    
    
    func setupCaputerSession (){
        captureSESSION.sessionPreset = AVCaptureSession.Preset.photo
    }
    func setUpDevice(){
        let devicediscoverySession =     AVCaptureDevice.discoverySession(deviceTypes:[AVCaptureDevice.DeviceType.builtInWideAngleCamera],mediaType: AVMediaType.video,position: AVCaptureDevice.Position.unspecified)
        
        let devices = devicediscoverySession.devices
        
        for device in devices {
            if device.position == AVCaptureDevice.Position.back {
                backCamera = device
            }
            else if device.position == AVCaptureDevice.Position.front {
                frontCamera = device
            }
            
        }
        
        currentCamera = backCamera
        
    }
    func setupinput(){
        
        
        do {
            let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
            caputreSession.addInput(captureDeviceInput)
            photoOutput = AVCapturePhotoOutput()
            photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey :  AVVideoCodecType.jpeg])],completionHandler: nil)
            captureSESSION.addOutput(photoOutput!)
        }
        catch{
            print(error)
        }
    }
    func setUpPreivewLayer(){
        
        
        cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: caputreSession)
        cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
        cameraPreviewLayer?.frame = self.view.frame
        self.view.layer.insertSublayer(cameraPreviewLayer!,at: 0)
    }
    func startRunningCaptiureSessions(){
        
        caputreSession.startRunning()
        
    }
    
    @objc func takePHoto(){
        
        let settings = AVCapturePhotoSettings()
        photoOutput?.capturePhoto(with: settings,delegate: self)
        

        
     pic.image =    photoOutput?.capturePhoto(with: settings,delegate: self)
   
    }
    
    
}
extension ViewController : AVCapturePhotoCaptureDelegate {
    func photoOutput(_ output: AVCapturePhotoOutput,didFinishProcessingPhoto photo: AVCapturePhoto,error: Error?) {
        if let imageData = photo.fileDataRepresentation(){
            
            image = UIImage(data: imageData)
            
                    let vc = previewVC()
                    vc.modalPresentationStyle = .overCurrentContext // actually .fullScreen would be better
                    self.present(vc,animated: true)
            
                    
                    vc.image = self.image
        }
        
    }
    
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。