Ich mache eine benutzerdefinierte Kamera-App, in der ich meine Fotos mit der Schaltfläche takePhoto erfassen kann, aber es gibt einen Fehler in takePhoto, dass AVCapturePhotoOutput kein Mitglied 'captureStillImageAsynchronous' hat.AVCapturePhotoOutput hat kein Mitglied 'captureStillImageAsynchronously'
Viewcontroller
import UIKit
import AVFoundation
import CoreImage
@available(iOS 10.0, *)
class ViewController: UIViewController {
var captureSession = AVCaptureSession()
var sessionOutput = AVCapturePhotoOutput()
var previewLayer = AVCaptureVideoPreviewLayer()
@IBOutlet weak var imageViewReal: UIImageView!
@IBOutlet weak var cameraView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let deviceSession = AVCaptureDeviceDiscoverySession.init(deviceTypes: [.builtInDuoCamera, .builtInTelephotoCamera, .builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .unspecified)
for device in (deviceSession?.devices)! {
if (device as AnyObject).position == AVCaptureDevicePosition.front {
do {
let input = try AVCaptureDeviceInput(device: device)
if captureSession.canAddInput(input) {
captureSession.addInput(input)
if captureSession.canAddOutput(sessionOutput){
captureSession.addOutput(sessionOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.connection.videoOrientation = .portrait
cameraView.layer.addSublayer(previewLayer)
cameraView.addSubview(takePhoto)
previewLayer.position = CGPoint (x: self.imageViewReal.frame.width/2, y: self.imageViewReal.frame.height/2)
previewLayer.bounds = imageViewReal.frame
captureSession.startRunning()
}
}
}
catch {
print("Error")
}
}
}
}
@IBAction func takePhoto(_ sender: Any) {
if let videoConnection = sessionOutput.connection(withMediaType: AVMediaTypeVideo) {
sessionOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: {
buffer, Error in
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
UIImageWriteToSavedPhotosAlbum(UIImage(data : imageData!)!, nil, nil, nil)
})
}
}
}
Drax Dank für Ihre Mühe, aber ich bekomme auch Fehler bei Zeile # 54 'cameraView.addSubview (TakePhoto)' errorr: 'kann nicht konvertieren den Wert von Typ Any zu UIview' –
Drax Dank für Ihre Mühe, aber ich bekomme auch Fehler bei Zeile # 54 cameraView.addSubview (takePhoto) errorr: Kann den Wert von Typ Any in UIview nicht konvertieren –