我正在使用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)
}
但我還沒有得到任何印刷錯誤。
你可以只添加一個UIImageView與捕獲圖像正確嗎? –
理論上,是的,但是「Here」這個詞沒有被打印在我的代碼中,所以它似乎沒有越過那個if語句。 – noblerare
你有沒有調用'captureSession?.startRunning()'?嘿,你可以發佈全長代碼嗎? –