DKImagePickerController
DKImagePickerController copied to clipboard
Limiting Video Length
How can I limit the video length in the same sense as UIImagePickerController setting the maximum video duration?
The DKImagePickerController
does not support video recording.
@zhangao0086 I'm talking about selecting a video from the phone gallery, In UIImagePickerController you can set the maximum video duration, if the selected video is longer than the maximum duration your'll be presented with something similar to UIVideoEditorController, which will force you to trim the video before selecting it. Implementing this manually will be something similar to this,
let maxDuration:Float64 = 60
let videoDuration = asset!.duration
let videoDurationSeconds = CMTimeGetSeconds(videoDuration)
if videoDurationSeconds > maxDuration
{
if UIVideoEditorController.canEditVideoAtPath(asset!.URL.path!)
{
let videoEditor = UIVideoEditorController()
self.videoEditor.videoPath = asset!.URL.path!
self.videoEditor.videoMaximumDuration = 60.0
self.presentViewController(self.videoEditor, animated: true, completion: nil)
}
}
// MARK: - UIVideoEditorControllerDelegate
func videoEditorController(editor: UIVideoEditorController, didSaveEditedVideoToPath editedVideoPath: String) {
self.videoEditor.dismissViewControllerAnimated(true, completion: nil)
//Do whatever you wish with the trimmed video here
}
Sounds good to me. I'll add it in the next version :)
@zhangao0086 Do you have implemented this ? Setting max duration of video that can be selected. for ex. we can select max 3 minutes video?
My fault. This feature has been supported. e.g. Gets less than 10 seconds of video:
pickerController.videoFetchPredicate = NSPredicate(format: "duration <= %f", 10.0)
pickerController.videoFetchPredicate = NSPredicate(format: "duration <= %f", 10.0), is not the same, as this will not show any video above 10 seconds, so basically the user will not see it in the picker, however the code I previously shared will show all videos and once selected will let the user trim the selected video to the desired duration
@islamabdelraouf Sorry for closing this issue. I've created a temporary branch Feature/ObservingAssetChanges
that just a demo project. Could you see it works as you expect?
Any update on this
Is there any update on this?
Any update on this ??
Any update on this? It seems videoFetchPredicate has also been removed in v4.2.1. Is there anything that has been added to replace this to limit the duration?
I even tried creating an object of DKImagePickerController with using custom configurations in the DKImageGroupDataManager, which failed to work. The code looks like so:
func createPickerController(forAssetType assetType: DKImagePickerControllerAssetType) -> DKImagePickerController {
let configurations = DKImageGroupDataManagerConfiguration()
let createImagePredicate = { () -> NSPredicate in
let imagePredicate = NSPredicate(format: "mediaType == %d",
PHAssetMediaType.image.rawValue)
return imagePredicate
}
let createVideoPredicate = { () -> NSPredicate in
let videoPredicate = NSPredicate(format: "mediaType == %d",
PHAssetMediaType.video.rawValue)
return videoPredicate
}
let createVideoDurationPredicate = { () -> NSPredicate in
let videoPredicate = NSPredicate(format: "duration <= %f", 240.0)
return videoPredicate
}
var predicate: NSPredicate?
switch assetType {
case .allAssets:
predicate = NSCompoundPredicate(orPredicateWithSubpredicates:
[createImagePredicate(), createVideoPredicate(), createVideoDurationPredicate()]
)
case .allPhotos:
predicate = createImagePredicate()
case .allVideos:
predicate = NSCompoundPredicate(orPredicateWithSubpredicates:
[createVideoPredicate(), createVideoDurationPredicate()]
)
}
let assetFetchOptions = PHFetchOptions()
assetFetchOptions.predicate = predicate
configurations.assetFetchOptions = assetFetchOptions
configurations.fetchLimit = 0
let manager = DKImageGroupDataManager(configuration: configurations)
let pickerController = DKImagePickerController(groupDataManager: manager)
pickerController.assetType = assetType
return pickerController
}
Hi @pramittewari Try this:
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [
NSPredicate(format: "mediaType == %d", PHAssetMediaType.video.rawValue),
NSPredicate(format: "duration < %d", 10),
])
let configuration = DKImageGroupDataManagerConfiguration()
configuration.assetFetchOptions = fetchOptions
let groupDataManager = DKImageGroupDataManager(configuration: configuration)
let pickerController = DKImagePickerController(groupDataManager: groupDataManager)
It will only give you videos less than 10 seconds.
I've done the same thing in the above function.
The difference I see is:
Mine says: "duration <= %f", 240.0 Yours says: "duration < %d", 10
Is this it?
From my test results:
"duration < %d", 10
works for me
"duration <= %f", 240.0
failed to work
Let me know if that works for you too :)
That's right. It's the only change that was required. Thanks for the promptness! =)
One more thing, does the new version (4.2.1) have any feature of displaying all videos but upon selection provide an option of trimming if longer than a particular duration?
@pramittewari No, it does not support trimming videos.
非常感谢您的三方,很好用,现在我遇到一个问题,希望在设置只有视频的时候点击相机能跳转到视频拍摄并获取拍摄的视频?我该怎么做呢?