I want to load all camera roll videos in UICollectionView along with their duration (same way, the videos are shown in camera roll). How to do so ? I checked this link. But they showed their videos like images.
Asked
Active
Viewed 7,062 times
3
-
Possible duplicate of [swift: How to load photos from photo library without using UIImagePickerController?](http://stackoverflow.com/questions/35115117/swift-how-to-load-photos-from-photo-library-without-using-uiimagepickercontroll) – Oren Edrich Feb 14 '17 at 06:14
2 Answers
11
// get all videos from Photos library you need to import Photos framework
var photos: PHFetchResult<PHAsset>! // user photos array in collectionView for disaplying video thumnail
func getAssetFromPhoto() {
let options = PHFetchOptions()
options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]
options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
photos = PHAsset.fetchAssets(with: options)
print(photos)
photoCollectionView.reloadData() // reload your collectionView
}
// For displaying thumnait image and video dutation
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotoCollectionViewCell
let asset = photos!.object(at: indexPath.row)
let width: CGFloat = 150
let height: CGFloat = 150
let size = CGSize(width:width, height:height)
PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: PHImageContentMode.aspectFill, options: nil) { (image, userInfo) -> Void in
self.photoImageView.image = image
self.lblVideoTime.text = String(format: "%02d:%02d",Int((asset.duration / 60)),Int(asset.duration) % 60)
}
return cell
}
- Note : Privacy - Photo Library Usage Description - > I need you photos library add this key into info.plist file

Timothy C.
- 639
- 1
- 5
- 10

Yogesh Makwana
- 448
- 4
- 16
-
1
-
I didn't get the second portion of your code...where to write that and the function **getAssetFromPhoto()**, should I call it in viewDidLoad() method ? – pigeon_39 Feb 14 '17 at 09:22
-
yes getAssetFromPhoto() call in viewDidLoad() and for displaying video thumnail : func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { } – Yogesh Makwana Feb 14 '17 at 09:37
-
can you please upload the **func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { }** for me it is tough to understand :( – pigeon_39 Feb 14 '17 at 09:46
-
-
-
I need to get the video URL when user select a UICollectionView cell. What should I write in **func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { }** ? Can you please help me on that? – pigeon_39 Feb 15 '17 at 09:10
-
i need to have a collection view where , images and videos all are mixed . how can we do it . i know how to do with only images or videos . how can we combine both ? – Saket Kumar Feb 26 '17 at 10:56
-
options.predicate = NSPredicate(format: "mediaType = %d && mediaType = %d”, PHAssetMediaType.video.rawValue,PHAssetMediaType.image.rawValue) – Yogesh Makwana Feb 26 '17 at 19:04
1
I recommend using the photos framework that apple has prebuilt into Xcode. The answer to this question is already filled out here:
swift: How to load photos from photo library without using UIImagePickerController?
The photos framework created by apple:
To add a video you can use a UIImagePickerCrollerSourceType:
picker.sourceType = UIImagePickerControllerSourceType.Camera

Community
- 1
- 1

Oren Edrich
- 674
- 1
- 7
- 23