我试图拥有一个UIImagePickerController
,可以通过简单的触摸拍摄照片,并以长时间的方式录制视频作为Snapchat。
这是我的UIImagePickerController
子类:
import UIKit
import MobileCoreServices
class ImagePickerController: UIImagePickerController, UIGestureRecognizerDelegate {
var takeButton : UIButton
override init ()
{
takeButton = UIButton ();
super.init()
self.sourceType = UIImagePickerControllerSourceType.Camera
self.allowsEditing = false
self.showsCameraControls = false
self.mediaTypes = [kUTTypeMovie, kUTTypeImage]
self.videoMaximumDuration = 10
self.videoQuality = UIImagePickerControllerQualityType.TypeMedium;
let screenSize: CGRect = UIScreen.mainScreen().bounds
self.cameraOverlayView?.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
takeButton.frame = CGRectMake(0, 0 , 100, 100)
takeButton.center = CGPointMake(screenSize.width/2, screenSize.height - 60);
takeButton.setImage(UIImage(named: "takeButton.png"), forState: UIControlState.Normal)
takeButton.addTarget(self, action: "takePicture:", forControlEvents: UIControlEvents.TouchUpInside)
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
{
let recognizer = UILongPressGestureRecognizer(target: self, action:Selector("holdAction:"))
recognizer.delegate = self
takeButton.addGestureRecognizer(recognizer)
}
self.cameraOverlayView?.clipsToBounds = true;
self.cameraOverlayView?.addSubview(takeButton)
}
func takePicture(sender:UIButton!)
{
self.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
self.takePicture()
}
func holdAction(recognizer: UILongPressGestureRecognizer)
{
self.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
if recognizer.state == UIGestureRecognizerState.Began
{
self.startVideoCapture()
println("Video capturing...")
}
else if recognizer.state == UIGestureRecognizerState.Ended
{
self.stopVideoCapture()
println("End recording !");
}
}
required init(coder aDecoder: NSCoder) {
takeButton = UIButton ();
super.init(coder: aDecoder)
}
private override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
takeButton = UIButton ();
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
}
图片部分没问题,但对于视频作品,它只能第二次工作。第一次触摸时,我总是遇到其中一个错误:
Camera: ignoring _previewStarted because waiting for session to be rebuilt
UIImagePickerController: requested to stop video capture before recording stopped
UIImagePickerController: ignoring request to stop video capture; camera is not currently capturing
视频
UIImagePickerController:忽略更改摄像头模式的请求;相机正在拍摄视频。
你对我的问题有所了解吗?
答案 0 :(得分:1)
实际上UIImagePickerController是一个非常高级的API,因此我们必须降低一个级别并使用 AVFoundation 并启动 AVCaptureSession 来执行此操作。希望它有所帮助!
有关AVFoundation的快速教程可以在这里找到:http://jamesonquave.com/blog/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-1/
感谢来自raywenderlich.com的 reidnez !