私はSwift 3、Xcode 8.2を使用しています。Swift 3 - カスタムカメラビュー - ボタンをクリックした後に撮影された写真の静止画像を表示
私はシャッターとして機能したいビデオフィードとボタンを表示するカスタムカメラビューを持っています。ユーザーがボタンをタップすると、撮影した画像を画面に表示します。 (例えばSnapchatやFacebookメッセンジャースタイルのカメラの動作など)
ここでは私のコードです:
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
に割り当てて、それをカメラのフィードの上に何とかオーバーレイしたいと思っています。
どのようなヘルプまたは正しい方向のポイントが大きな助けになるでしょう。私のコードに以下を追加した後
EDIT
:
if let error = error {
print(error.localizedDescription)
}
しかし、私はまだエラーが印刷されません。
あなただけのキャプチャとUIImageViewを追加することができますイメージ右? –
理論的にはyesですが、 "Here"という単語は私のコードでは印刷されていないので、 'if'ステートメントを過ぎているようには見えません。 – noblerare
'captureSession?startRunning()'を呼び出しましたか?ちょっと全長コードを投稿できますか? –