【发布时间】:2026-02-16 05:00:02
【问题描述】:
我正在开发一个需要自定义相机 UI 的应用程序。所以我在 ImagePickerController 中使用了 Overlay 视图。用户可以在捕捉模式之间切换。
(UIImagePickerControllerCameraCaptureMode.Video 和 UIImagePickerControllerCameraCaptureMode.Photo)
我的问题是当用户将捕获模式从.Photo 切换到.Video startVideoCapture() 函数需要时间(最多 5 秒)来初始化视频捕获在第一次或第二次。
有时它不会在指定路径上保存视频。.Photo 模式工作正常。
在开始时,我使用 .Photo 模式初始化了 imagePickerController。用户可以将 .Photo 切换为 .Video。我认为可能正在初始化 .Video 模式需要时间。为此,我使用 .Video 模式初始化 imagePickerController 并设置一个计时器以在 4 秒内将 .video 更改为 .Photo 模式。虽然这 4 秒用户看不到相机,但我添加了加载屏幕以停止用户交互。
但问题仍然存在。 startVideoCapture() 第一次花时间开始录制。并且有时在录制后它不会将视频保存在指定路径上。
这是我在委托方法中保存视频的代码。
/**
* 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
请有人帮忙。
【问题讨论】:
标签: ios swift camera uiimagepickercontroller video-recording