Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

swift - capture photo from AvcaputrePhotoOutout to place on imageview

I want my swift code to take a photo and place it on the imageview pic. You can see what I did in func take Photo. The code is causing a run time error when called I dont know what to place in the code for it to not cause a run time error and to take the photo and place it on the imageview pic. I think i have most of the needed code below.

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, width: 100, 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
        }
        
    }
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...