用自定义相机拍照

时间:2017-01-01 15:24:25

标签: ios swift

我正在尝试拍摄自定义相机视图,代码不显示任何错误 但在我的imageview中仍然没有发生......

@IBAction func photoTake(_ sender: UIButton) {


        func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

            if let error = error {
                print(error.localizedDescription)
            }

            if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer,
                let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {



                let dataProvider = CGDataProvider(data: dataImage as CFData)

                let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.absoluteColorimetric)


                let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)





                print(UIScreen.main.bounds.width)


                self.capturedimage.image = image
                self.capturedimage.isHidden = false



            } else {

            }
        }

    }

我从这个帖子中获取代码 Taking photo with custom camera Swift 3

1 个答案:

答案 0 :(得分:0)

Solutuion Swift 3

if let videoConnection = stillImageOutput.connection(withMediaType:AVMediaTypeVideo){

            stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (imageDataSampleBuffer, error) in

                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
                let image = UIImage(data: imageData!)
                print("image Taked: \(image)")





            })

        }