我正在开发一个需要自定义相机 UI 的应用程序。所以我在 ImagePickerController 中使用了 Overlay View 。用户可以在捕捉模式之间切换。
(UIImagePickerControllerCameraCaptureMode.Video
和 UIImagePickerControllerCameraCaptureMode.Photo
)
我的问题是,当用户将捕获模式从 .Photo
切换到 .Video
startVideoCapture()
函数时需要时间(最多 5 秒) ) 初始化第一次或第二次视频采集。
有时它不会将视频保存在指定路径上。.Photo
模式工作正常。
一开始我用 .Photo 模式初始化了 imagePickerController。用户可以将.Photo 切换为.Video。我认为可能正在初始化。视频模式会花费一些时间。为此,我使用 .Video 模式初始化 imagePickerController,并设置一个计时器以在 4 秒内将 .video 更改为 .Photo 模式。虽然这 4 秒用户无法看到相机,因为我添加了加载屏幕来停止用户交互。
但问题仍然存在。 startVideoCapture()
第一次开始录制需要一些时间。并且有时录制后不会将视频保存到指定路径。
这是我在委托(delegate)方法中保存视频的代码。
/**
* Delegate method for media capture and save media
*/
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[NSObject : AnyObject]) {
overlayView.bringSubviewToFront(preView)
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
if mediaType.isEqualToString(kUTTypeMovie as NSString as String){
let tempMedia = info[UIImagePickerControllerMediaURL] as! NSURL!
let pathString = tempMedia.relativePath
if pathString != nil {
destinationPathForVideo = "\(UtilityManager.sharedInstance.kcache)/challengeRecordings/\(NSDate()).mp4"
NSFileManager.defaultManager().createDirectoryAtPath(destinationPathForVideo.stringByDeletingLastPathComponent, withIntermediateDirectories: true, attributes: nil, error: nil)
UtilityManager.sharedInstance.copyFileFromSourceTo( pathString, destinationPath: destinationPathForVideo )
// Save Image
imageCaptured = getThumbnailForVideoPath(pathString!)
destinationPathForImage = "\(UtilityManager.sharedInstance.kcache)/challengeRecordings/\(NSDate()).png"
let success = UIImagePNGRepresentation(imageCaptured).writeToFile( destinationPathForImage , atomically: true)
Log(message:"Saved: \(success)")
imageButton!.setImage(imageCaptured, forState: .Normal)
imageView.image = imageCaptured
}
}
这是我的初始化代码
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
imagePicker.showsCameraControls = false
self.imagePicker.mediaTypes = [kUTTypeMovie, kUTTypeImage]
self.imagePicker.videoMaximumDuration = 11
self.imagePicker.videoQuality = UIImagePickerControllerQualityType.TypeMedium
self.imagePicker.cameraFlashMode = .Off
self.imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video
请有人帮忙。
最佳答案
我发现了一个很棒的库,其中包含所有 mediapickercontroller 功能。你应该试试。 IQMediaPickerController
关于ios - UIImagePickerController 视频录制在延迟或不工作后开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32775831/