Capturez l'image de l'appareil photo iOS

J'ai besoin de capturer une image à travers l'appareil photo. Mais il donne l'erreur suivante: 'NSInvalidArgumentException', raison: 'Impossible d'instancier un AVCaptureDevice directement.'

Suit le code:

var session: AVCaptureSession! var device: AVCaptureDevice! var captureVideoPreviewLayer: AVCaptureVideoPreviewLayer! var image: UIImage! var input: AVCaptureDeviceInput! var stillImageOutput: AVCaptureStillImageOutput! var viewControllerScheduling: RentalSchedulingViewController! override func viewDidLoad() { super.viewDidLoad() session = AVCaptureSession() session.sessionPreset = AVCaptureSessionPresetPhoto; captureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: session) captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill captureVideoPreviewLayer.frame = self.view.frame self.view.layer.addSublayer(captureVideoPreviewLayer) var error = NSErrorPointer() captureVideoPreviewLayer.session.beginConfiguration() input = AVCaptureDeviceInput(device: self.getBackCamera(), error: error) session.addInput(input) captureVideoPreviewLayer.session.commitConfiguration() stillImageOutput = AVCaptureStillImageOutput() var outputSettings = NSDictionary(objectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey) stillImageOutput.outputSettings = outputSettings session.addOutput(stillImageOutput) session.startRunning() } 

Récupérer la camera:

  func getBackCamera() -> AVCaptureDevice { var devic = AVCaptureDevice() let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as NSArray for dev in devices { devic = dev as AVCaptureDevice if devic.position == AVCaptureDevicePosition.Back { return devic } } return devic } 

Événement pour capturer l'image:

  @IBAction func capture(sender: AnyObject) { var videoConnection = AVCaptureConnection() for con in stillImageOutput.connections { let connection = con as AVCaptureConnection for p in connection.inputPorts { let port = p as AVCaptureInputPort if port.mediaType == AVMediaTypeVideo { videoConnection = connection break; } } } stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) { (imageSampleBuffer, error) in if imageSampleBuffer != nil { let imageData = AVCaptureStillImageOutput .jpegStillImageNSDataRepresentation(imageSampleBuffer) self.processImage(UIImage(data: imageData)!) } } } 

L'image de traitement:

  func processImage(image: UIImage) { UIGraphicsBeginImageContext(CGSizeMake(320, 426)); image.drawInRect(CGRectMake(0, 0, 320, 426)) let smallImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext(); let cropRect = CGRectMake(0, 55, 320, 320) let imageRef = CGImageCreateWithImageInRect(smallImage.CGImage, cropRect) self.image = UIImage(CGImage: imageRef) } 

l'erreur se produit ici:

 input = AVCaptureDeviceInput(device: self.getBackCamera(), error: error) 

J'ai recherché ce problème. Il semble que vous ne pouvez pas réellement instancier l'object périphérique AVCaptureDevice. dois le passer par reference. C'est ainsi que j'ai surmonté le défi. Je n'ai pas instancié la class que je l'ai simplement passée en reference à l'object AVCaptureDeviceInput.

J'espère que cela a aidé!

  var captureDevice : AVCaptureDevice? let devices = AVCaptureDevice.devices() // Loop through all the capture devices on this phone for device in devices { // Make sure this particular device supports video if (device.hasMediaType(AVMediaTypeVideo)) { // Finally check the position and confirm we've got the back camera if(device.position == AVCaptureDevicePosition.Back) { captureDevice = device as? AVCaptureDevice } } } } 

Peut-être que vous le résolvez en attendant, mais si vous changez votre code comme ça, ça devrait marcher:

Supprimez simplement le devic = AVCaptureDevice() dans votre getBackCamera()

et faites la déclaration dans la tête de la class comme ceci: var device, devic : AVCaptureDevice!

 class ViewController: UIViewController { var session: AVCaptureSession! var device, devic : AVCaptureDevice! var captureVideoPreviewLayer: AVCaptureVideoPreviewLayer! var image: UIImage! var input: AVCaptureDeviceInput! var stillImageOutput: AVCaptureStillImageOutput! //var viewControllerScheduling: RentalSchedulingViewController! override func viewDidLoad() { super.viewDidLoad() session = AVCaptureSession() session.sessionPreset = AVCaptureSessionPresetPhoto; captureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: session) captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill captureVideoPreviewLayer.frame = self.view.frame self.view.layer.addSublayer(captureVideoPreviewLayer) var error = NSErrorPointer() captureVideoPreviewLayer.session.beginConfiguration() input = AVCaptureDeviceInput(device: self.getBackCamera(), error: error) session.addInput(input) captureVideoPreviewLayer.session.commitConfiguration() stillImageOutput = AVCaptureStillImageOutput() var outputSettings = NSDictionary(objectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey) stillImageOutput.outputSettings = outputSettings as [NSObject : AnyObject] session.addOutput(stillImageOutput) session.startRunning() } func getBackCamera() -> AVCaptureDevice { // devic = AVCaptureDevice() let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as NSArray for dev in devices { devic = dev as! AVCaptureDevice if devic.position == AVCaptureDevicePosition.Back { return devic } } return devic }