2015-01-05 108 views
4

我試圖捕獲圖像,並在按「myButton」時將其保存到變量中。我該怎麼辦?使用AVFoundation捕獲當前攝像頭圖像

我的代碼如下:

import UIKit 
import AVFoundation 
import MobileCoreServices 

class ViewController: UIViewController { 

    let captureSession = AVCaptureSession() 
    var previewLayer : AVCaptureVideoPreviewLayer? 
    var captureDevice : AVCaptureDevice? 
    @IBOutlet var myTap: UITapGestureRecognizer! 
    @IBOutlet weak var myButton: UIButton! 

    @IBAction func shotPress(sender: UIButton) { 
     //Save image to variable somehow 
     }) 
     var stillImageOutput = AVCaptureStillImageOutput() 
     stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG] 

     if captureSession.canAddOutput(stillImageOutput) { 
      captureSession.addOutput(stillImageOutput) 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     captureSession.sessionPreset = AVCaptureSessionPresetHigh 
     let devices = AVCaptureDevice.devices() 
     for device in devices { 
      if (device.hasMediaType(AVMediaTypeVideo)) { 
       if(device.position == AVCaptureDevicePosition.Back) { 
        captureDevice = device as? AVCaptureDevice 
        if captureDevice != nil { 
         beginSession() 
        } 
       } 
      } 
     } 
    } 

    func updateDeviceSettings(focusValue : Float, isoValue : Float) { 
     if let device = captureDevice { 
      if(device.lockForConfiguration(nil)) { 
       device.focusMode = AVCaptureFocusMode.ContinuousAutoFocus 
       device.unlockForConfiguration() 
      } 
     } 
    } 

    func beginSession() { 
     var err : NSError? = nil 
     captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err)) 
     if err != nil { 
      println("error: \(err?.localizedDescription)") 
     } 
     previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
     self.view.layer.addSublayer(previewLayer) 
     self.view.bringSubviewToFront(myButton) 
     previewLayer?.frame = self.view.layer.frame 
     captureSession.startRunning() 
    } 

} 
+0

什麼你基本上做的是試圖抓住這個形象samplebuffer。你可以通過在AVCaptureStillImageOutput上調用'func captureStillImageAsynchronouslyFromConnection(_connection:AVCaptureConnection!,completionHandler handler:((CMSampleBuffer!,NSError!) - > Void)!)'來實現。通過一個完成處理程序,在其中你會做你想要的圖像。 – Jan

回答

8

這是我使用,我工作的應用程序的一個東西。應該對你的問題也有幫助。

func capturePicture(){ 

    println("Capturing image") 
    stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG] 
    captureSession.addOutput(stillImageOutput) 

    if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo){ 
      stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { 
        (sampleBuffer, error) in 
       var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) 
       var dataProvider = CGDataProviderCreateWithCFData(imageData) 
       var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault) 
       var image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right) 

       var imageView = UIImageView(image: image) 
       imageView.frame = CGRect(x:0, y:0, width:self.screenSize.width, height:self.screenSize.height) 

       //Show the captured image to 
       self.view.addSubview(imageView) 

       //Save the captured preview to image 
       UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) 

       }) 
     } 
} 
+0

很好的答案。我可以問個問題嗎?什麼是kCGRenderingDefault? –

+0

您好,您可以使用var的imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) VAR圖像= UIImage的替換下面的成像數據的線(數據:爲imageData,刻度:1.0) //捕獲的預覽保存到圖像 UIImageWriteToSavedPhotosAlbum(圖像!無,無,無) – Morganster

+0

@MarcusGabilheri我想你想要:CGColorRenderingIntent.RenderingIntentDefault – iksnae

1

更新了斯威夫特3:

var stillImageOutput = AVCaptureStillImageOutput.init() 
stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG] 
self.cameraSession.addOutput(stillImageOutput) 

if let videoConnection = stillImageOutput.connection(withMediaType:AVMediaTypeVideo){ 
    stillImageOutput.captureStillImageAsynchronously(from:videoConnection, completionHandler: { 
    (sampleBuffer, error) in 
    var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) 
    var dataProvider = CGDataProvider.init(data: imageData as! CFData) 
    var cgImageRef = CGImage.init(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent) 
    var image = UIImage.init(cgImage: cgImageRef!, scale: 1.0, orientation: .right) 
    // do something with image 
    }) 
}