如何在iOS8.1上使用Swift拍摄多张照片(每张照片间隔1秒)的连续拍摄?

5
我正在尝试在单击相机预览后拍摄几张照片,以便我可以呈现它们并且用户可以选择最好的一张或使用“胶片条”模式中的所有照片。期望的用户体验是:“我打开相机,拍了一张照片,然后我看到按秒计算的5张照片。我不必按5次'拍照'按钮,只需要按一次即可开始序列。”
我是iOS和Swift的新手,我基于“Swift Recipes”(https://www.safaribooksonline.com/library/view/ios-8-swift/9781491908969/)书籍进行工作。
拍摄单张照片的源代码如下:
controller = UIImagePickerController()
    if let theController = controller{
        theController.sourceType = .Camera
        theController.mediaTypes = [kUTTypeImage as NSString]
        theController.allowsEditing = true
        theController.delegate = self
        presentViewController(theController, animated: true, completion: nil)
    }

还有相关的图像处理(通过func imagePickerController)。我尝试使用上面的控制器对象进行操作,但是失败了:( 我相信这不是我寻找的正确方法,但我很难找到正确的方法。任何帮助将不胜感激。

1个回答

5

我不知道UIImagePickerController是否有这种行为的模式。如果是我,我会使用AVFoundation来实现这个功能。

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var connection: AVCaptureConnection!
    var output: AVCaptureStillImageOutput!
    var videoPreviewLayer: AVCaptureVideoPreviewLayer!
    @IBOutlet var videPreviewView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.createCamera()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        self.videoPreviewLayer.bounds = self.videPreviewView.bounds
        self.videoPreviewLayer.position = CGPoint(x: CGRectGetMidX(self.videPreviewView.bounds), y: CGRectGetMidY(self.videPreviewView.bounds))
    }

    func createCamera() {
        let captureSession = AVCaptureSession()

        if captureSession.canSetSessionPreset(AVCaptureSessionPresetHigh) {
            captureSession.sessionPreset = AVCaptureSessionPresetHigh
        } else {
            println("Error: Couldn't set preset = \(AVCaptureSessionPresetHigh)")
            return
        }

        let cameraDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

        var error: NSError?
        let inputDevice = AVCaptureDeviceInput.deviceInputWithDevice(cameraDevice, error: &error) as AVCaptureDeviceInput
        if let error = error {
            println("Error: \(error)")
            return
        }

        if captureSession.canAddInput(inputDevice) {
            captureSession.addInput(inputDevice)
        } else {
            println("Error: Couldn't add input device")
            return
        }

        let imageOutput = AVCaptureStillImageOutput()
        if captureSession.canAddOutput(imageOutput) {
            captureSession.addOutput(imageOutput)
        } else {
            println("Error: Couldn't add output")
            return
        }

        // Store imageOutput. We will need it to take photo
        self.output = imageOutput

        let connection = imageOutput.connections.first as AVCaptureConnection!
        // Store this connection in property. We will need it when we take image.
        self.connection = connection
        connection.videoOrientation = AVCaptureVideoOrientation.Portrait

        captureSession.startRunning()

        // This will preview the camera
        let videoLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        videoLayer.contentsScale = UIScreen.mainScreen().scale

        self.videPreviewView.layer.addSublayer(videoLayer)

        // Store this layer instance in property. We will place it into a view
        self.videoPreviewLayer = videoLayer
    }

    func takePhoto(completion: (UIImage?, NSError?) -> Void) {
        self.output.captureStillImageAsynchronouslyFromConnection(self.connection) { buffer, error in
            if let error = error {
                completion(nil, error)
            } else {
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
                let image = UIImage(data: imageData, scale: UIScreen.mainScreen().scale)
                completion(image, nil)
            }
        }
    }
}

现在你需要创建一个调用takePhoto函数3次的操作。你可以使用NSTimer来实现。


如何使用UIButton调用takePhoto? - Slavcho

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接