私は迅速にライブフィルタカメラアプリを構築しています。SwiftのAVFoundationで写真をキャプチャ
私は、写真撮影機能を実行するためにAVCaptureVideoDataOutput()からAVCaptureStillImageOutput()に変更しています。私が変更した後、私はアプリを開くと、プレビュービューがありません。
メインのグループ領域をクリックすると、キャプチャ写真機能が動作しているので、キャプチャサウンド "Ka"を聞くことができます。ここには見解はありません。
は、ここに私の完全なコード
import Foundation
import UIKit
import AVFoundation
import CoreMedia
let CIHueAdjust = "CIHueAdjust"
let CIHueAdjustFilter = CIFilter(name: "CIHueAdjust", withInputParameters: ["inputAngle" : 1.24])
let Filters = [CIHueAdjust: CIHueAdjustFilter]
let FilterNames = [String](Filters.keys).sort()
class LiveCamViewController : UIViewController,AVCaptureVideoDataOutputSampleBufferDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let mainGroup = UIStackView()
let imageView = UIImageView(frame: CGRectZero)
let filtersControl = UISegmentedControl(items: FilterNames)
var videoOutput = AVCaptureStillImageOutput()
override func viewDidLoad()
{
super.viewDidLoad()
view.addSubview(mainGroup)
mainGroup.axis = UILayoutConstraintAxis.Vertical
mainGroup.distribution = UIStackViewDistribution.Fill
mainGroup.addArrangedSubview(imageView)
mainGroup.addArrangedSubview(filtersControl)
mainGroup.addGestureRecognizer(UITapGestureRecognizer(target: self, action:#selector(LiveCamViewController.saveToCamera(_:))))
imageView.contentMode = UIViewContentMode.ScaleAspectFit
filtersControl.selectedSegmentIndex = 0
let captureSession = AVCaptureSession()
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
do
{
let input = try AVCaptureDeviceInput(device: backCamera)
captureSession.addInput(input)
}
catch
{
print("can't access camera")
return
}
//get captureOutput invoked
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(previewLayer)
videoOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if captureSession.canAddOutput(videoOutput)
{
captureSession.addOutput(videoOutput)
}
captureSession.startRunning()
}
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!)
{
guard let filter = Filters[FilterNames[filtersControl.selectedSegmentIndex]] else
{
return
}
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let cameraImage = CIImage(CVPixelBuffer: pixelBuffer!)
filter!.setValue(cameraImage, forKey: kCIInputImageKey)
let filteredImage = UIImage(CIImage: filter!.valueForKey(kCIOutputImageKey) as! CIImage!)
let fixedImage = correctlyOrientedImage(filteredImage)
dispatch_async(dispatch_get_main_queue())
{
self.imageView.image = fixedImage
}
}
func correctlyOrientedImage(image: UIImage) -> UIImage {
UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
let imageRef: CGImageRef = normalizedImage.CGImage!
let rotatedImage: UIImage = UIImage(CGImage: imageRef, scale: 1.0, orientation: .Right)
return rotatedImage
}
override func viewDidLayoutSubviews()
{
mainGroup.frame = CGRect(x: 37, y: 115, width: 301, height: 481)
}
func saveToCamera(sender: UITapGestureRecognizer) {
videoOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if let videoConnection = videoOutput.connectionWithMediaType(AVMediaTypeVideo) {
videoOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData)!, nil, nil, nil)
}
}
}
}
おかげです。
スウィフトにAVCaptureSessionで画像をキャプチャする[方法の可能性のある重複を? ](http://stackoverflow.com/questions/28756363/how-to-capture-picture-with-avcapturesession-in-swift) – iYoung
あなたはこれを参照できますhttp://stackoverflow.com/questions/25871513/swift-take -photo-from-avfoundation?rq = 1 – iYoung
音が聞こえる場合。おそらくあなたのコードはAvfoundationのために働いています。あなたのビューレイアウト(autolayout)またはアウトレットは、 – Shubhank