简体   繁体   English

迅速的照片捕捉黑色图像

[英]swift photo capture black image

I'm trying to set up AVFoundation photo capture using Swift. 我正在尝试使用Swift设置AVFoundation照片捕获。 The preview window works fine but when I press the button to capture the photo, the first picture taken after the app is launched is totally black. 预览窗口可以正常工作,但是当我按下按钮捕获照片时,启动该应用程序后拍摄的第一张照片完全是黑色的。 Any subsequent pictures I take in the same instance of the app running work correctly, but that first one is always just a black rectangle. 我在运行该应用程序的同一实例中拍摄的所有后续照片均能正常工作,但第一个照片始终只是一个黑色矩形。 The preview layer also flashes black when the picture is taken. 拍摄照片时,预览层也会闪烁黑色。 Here's the code I'm using for the camera: 这是我用于相机的代码:

import UIKit
import AVFoundation

class photoCaptureViewController: UIViewController {

let session                     : AVCaptureSession = AVCaptureSession()
var previewLayer                : AVCaptureVideoPreviewLayer!
var output = AVCaptureStillImageOutput()
var capturedImage               : UIImage!

override func viewDidLoad() {
    super.viewDidLoad()

  let device = getFrontCamera()
  var error   : NSError? = nil

  let input   : AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(device, error: &error) as? AVCaptureDeviceInput

  if input != nil {
    session.addInput(input)
  }else{
    println(error)
  }

  session.sessionPreset = AVCaptureSessionPresetPhoto

  previewLayer = AVCaptureVideoPreviewLayer.layerWithSession(session) as! AVCaptureVideoPreviewLayer
  previewLayer.frame = CGRectMake(0, 0, 300, 300)
  previewLayer.setAffineTransform( CGAffineTransformTranslate(CGAffineTransformMakeScale(0.33, 0.33), -375, -480) )
  previewLayer.position = CGPointMake((photoButton.layer.position.x + 70), (photoButton.layer.position.y - 45))

  previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

  self.view.layer.addSublayer(previewLayer)

  self.setPreviewOrientation()

  session.startRunning()

}

func setPreviewOrientation() {
  let previewLayerConnection: AVCaptureConnection = self.previewLayer.connection
  let orientation = AVCaptureVideoOrientation(rawValue: UIDevice.currentDevice().orientation.rawValue)
  previewLayerConnection.videoOrientation = orientation!
}

@IBAction func photoButtonPressed(sender: UIButton)
{

  output.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

  if session.canAddOutput(output)
  {
    session.addOutput(output)
  }

  var videoConnection = output.connectionWithMediaType(AVMediaTypeVideo)

  if videoConnection != nil {
    output.captureStillImageAsynchronouslyFromConnection(output.connectionWithMediaType(AVMediaTypeVideo))
      { (imageDataSampleBuffer, error) -> Void in
        self.capturedImage = UIImage(data: AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer))

        UIImageWriteToSavedPhotosAlbum(self.capturedImage, self,nil, nil)

    }}


}

func getFrontCamera() -> AVCaptureDevice{
  var result : AVCaptureDevice!
  var devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
  for device in devices{
    if (device.position == AVCaptureDevicePosition.Front) {
      result = device as! AVCaptureDevice
    }
  }
  return result
}

}

This was fixed by moving the 这是通过移动

 output.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

if session.canAddOutput(output)
{
  session.addOutput(output)
}

up under viewDidLoad instead of having it in the photo capture function. 而不是将其包含在照片捕获功能中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM