我一直在使用自定义相机,最近我和Swift 3一起升级到Xcode 8 beta.我原来是这样的:
var stillImageOutput: AVCaptureStillImageOutput?
Run Code Online (Sandbox Code Playgroud)
但是,我现在收到警告:
在iOS 10.0中不推荐使用'AVCaptureStillImageOutput':改用AVCapturePhotoOutput
由于这是相当新的,我没有看到很多关于此的信息.这是我目前的代码:
var captureSession: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
func clickPicture() {
if let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo) {
videoConnection.videoOrientation = .portrait
stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
if sampleBuffer != nil {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProvider(data: imageData!)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let image = UIImage(cgImage: cgImageRef!, scale: 1, orientation: .right)
}
})
}
} …Run Code Online (Sandbox Code Playgroud)