How To Use AVCaptureStillImageOutput To Take Picture

前端 未结 4 1648
挽巷
挽巷 2021-02-04 08:35

I have a preview layer that is pulling from the camera and working as it should. I would like to be able to take a picture when I press a button. I have inited the AVCaptureStil

4条回答
  •  [愿得一人]
    2021-02-04 09:00

    for swift version:

    @IBAction func capture(sender: AnyObject) {
    
        var videoConnection :AVCaptureConnection?
    
        if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo){
            stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (buffer:CMSampleBuffer!, error: NSError!) -> Void in
    
                if let exifAttachments = CMGetAttachment(buffer, kCGImagePropertyExifDictionary, nil) {
                    let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
                    self.previewImage.image = UIImage(data: imageData)
                    UIImageWriteToSavedPhotosAlbum(self.previewImage.image, nil, nil, nil)
                }
            })
        }
    }
    

提交回复
热议问题