我是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).我尝试使用上面的控制器目标,但悲惨地失败了:(我相信这不是正确的方式来获得我想要的,但我很难找到合适的.任何帮助将不胜感激.
解决方法 我不知道UIImagePickerController中是否存在此行为的模式.如果我是你,我会用 AVFoundation来实现这个.import UIKitimport AVFoundationclass 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执行此 *** 作.
总结以上是内存溢出为你收集整理的如何在iOS8.1上使用Swift按序列拍摄多张照片(每张照片延迟1秒)?全部内容,希望文章能够帮你解决如何在iOS8.1上使用Swift按序列拍摄多张照片(每张照片延迟1秒)?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)