2017-03-02 48 views
1

我正在使用Swift 3,Xcode 8.2。Swift 3 - 自定義相機視圖 - 顯示按鈕單擊後拍攝的照片靜止圖像

我有一個自定義相機視圖,它顯示視頻輸入罰款和一個按鈕,我想作爲一個快門。當用戶點擊按鈕時,我想拍攝一張照片並將其顯示在屏幕上。 (例如像Snapchat或Facebook Messenger的風格攝像機行爲)

這裏是我的代碼:

import UIKit 
import AVFoundation 

class CameraVC: UIViewController, AVCapturePhotoCaptureDelegate { 

    // this is where the camera feed from the phone is going to be displayed 
    @IBOutlet var cameraView : UIView! 

    var shutterButton : UIButton = UIButton.init(type: .custom) 

    // manages capture activity and coordinates the flow of data from input devices to capture outputs. 
    var capture_session = AVCaptureSession() 

    // a capture output for use in workflows related to still photography. 
    var session_output = AVCapturePhotoOutput() 

    // preview layer that we will have on our view so users can see the photo we took 
    var preview_layer = AVCaptureVideoPreviewLayer() 

    // still picture image is what we show as the picture taken, frozen on the screen 
    var still_picture_image : UIImage! 

    ... //more code in viewWillAppear that sets up the camera feed 

    // called when the shutter button is pressed 
    func shutterButtonPressed() { 

     // get the actual video feed and take a photo from that feed 
     session_output.capturePhoto(with: AVCapturePhotoSettings.init(format: [AVVideoCodecKey : AVVideoCodecJPEG]), delegate: self as AVCapturePhotoCaptureDelegate) 
    } 

    func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { 

     // take the session output, get the buffer, and create an image from that buffer 
     if let sampleBuffer = photoSampleBuffer, 
      let previewBuffer = previewPhotoSampleBuffer, 
      let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) { 

      print("Here") // doesn't get here 

     } 

    } 

它似乎沒有運行此代碼時打印「在這裏」,我無法找到任何斯威夫特3教程如何顯示此圖像。我猜想我想採取imageData並將其分配給我的still_picture_image並以某種方式疊加在相機供稿上。

任何幫助或正確方向的一個點將是很大的幫助。

編輯

添加以下到我的代碼後:

 if let error = error { 
      print(error.localizedDescription) 
     } 

但我還沒有得到任何印刷錯誤。

+0

你可以只添加一個UIImageView與捕獲圖像正確嗎? –

+0

理論上,是的,但是「Here」這個詞沒有被打印在我的代碼中,所以它似乎沒有越過那個if語句。 – noblerare

+0

你有沒有調用'captureSession?.startRunning()'?嘿,你可以發佈全長代碼嗎? –

回答

0

添加以下代碼到你的委託方法來打印出被拋出的錯誤:

if let error = error { 
    print(error.localizedDescription) 
} 

一旦你讓你的錯誤得到解決,我覺得這個職位應該幫助你提取圖像:Taking photo with custom camera Swift 3

+0

好的,我更新了我原來的帖子。 – noblerare

0

好吧,我想出了我的問題:

首先,將UIImageView拖到故事板並佔用整個屏幕。這是按下快門按鈕後將顯示靜止圖像的地方。

在代碼中創建該變量並將其鏈接起來。

@IBOutlet weak var stillPicture : UIImageView! 

然後,在viewDidLoad確保您的相機視圖的頂端插入UIImageView

self.view.insertSubview(stillPicture, aboveSubview: your_camera_view) 

這是功能被點擊快門按鈕時調用:

func shutterButtonPressed() { 

    let settings = AVCapturePhotoSettings() 

    let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first! 
    let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType, 
         kCVPixelBufferWidthKey as String: 160, 
         kCVPixelBufferHeightKey as String: 160, 
         ] 
    settings.previewPhotoFormat = previewFormat 
    session_output.capturePhoto(with: settings, delegate: self) 
} 

然後,在你捕捉委託:

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { 
     if let error = error { 
      print(error.localizedDescription) 
     } 

     // take the session output, get the buffer, and create an image from that buffer 
     if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) { 
      // this is the image that the user has taken! 
      let takenImage : UIImage = UIImage(data: dataImage)! 
      stillPicture?.image = takenImage     
     } else { 
      print("Error setting up photo capture") 
     } 
} 
相關問題