Reputation: 509
I'm trying to set up AVFoundation photo capture using Swift. 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
}
}
Upvotes: 0
Views: 1690
Reputation: 509
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.
Upvotes: 1