ios - 如何在 iOS8.1 上使用 Swift 按顺序拍摄多张照片(每张延迟 1 秒)?

标签 ios swift ios8 ios-camera

我试图在单个用户点击相机预览后拍几张照片,这样我就可以展示它们,用户可以选择时间最佳的照片或在“胶卷”模式下全部使用。预期的用户体验是:“我打开相机,拍照,然后我看到一秒一秒拍了 5 张照片。我不必按 5 次‘拍照’按钮,一次就足以启动序列” .

我是 iOS 和 Swift 的新手,我的工作基于一本“Swift 食谱”书 (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)。我尝试使用上面的 Controller 对象,但惨遭失败 :( 我认为这不是获得我想要的东西的正确方法,但我很难找到合适的方法。我们将不胜感激。

最佳答案

我不知道 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 来执行此操作。

关于ios - 如何在 iOS8.1 上使用 Swift 按顺序拍摄多张照片(每张延迟 1 秒)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26641443/

相关文章:

ios - 在iOS中安装Jmeter证书

ios - 如何检查 swift 2.2 中是否存在 iphone 控制中心?

swift - 将 UnsafeMutableRawPointer 转换为 UInt64

swift - 定时器如何在 Swift 中工作?

uicollectionview - Swift 中自定义 UICollectionViewCell 上的标 checkout 口导致 Optional.None 崩溃

ios - 更新 UITableView 内的 UIView 帧大小 - swift

ios - swift 3 : UITableViewRowActionStyle() "Missing Parameter" Error Msg

ios - 如何使用 Linux openssl 为 iOS 生成 CSR?

ios - 使用谓词从数组中获取无序数据

ios - 如何为多个图像扩展注册iOS应用?