首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS8.1上使用Swift按顺序拍摄多张照片(每次延迟1s)?

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

Stack Overflow用户
提问于 2014-10-29 22:02:14
回答 1查看 4.3K关注 0票数 5

我试图在一个用户点击相机预览后拍摄几张照片,这样我就可以呈现它们,用户可以选择一个时机最好的照片,或者在“胶片条”模式下使用所有的照片。预期的用户体验是:“我打开相机,拍一张照片,然后一秒看到5张照片。我不需要按5下‘拍照’按钮5次,其中一个就足够启动序列了。”

我是iOS和Swift的新手,我的工作是基于一本“Swift”的书(https://www.safaribooksonline.com/library/view/ios-8-swift/9781491908969/)。

拍摄一张照片的源代码是:

代码语言:javascript
复制
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)。我试着和上面的控制器玩,但不幸的是失败了:(我相信这不是我想要的东西,但我很难找到正确的方法。)任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-30 00:01:51

我不知道在UIImagePickerController中是否存在这种行为的模式。如果我是您,我将使用AVFoundation来实现这个功能。

代码语言:javascript
复制
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来完成此操作。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26641443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档