【发布时间】:2017-02-14 05:56:34
【问题描述】:
我想在 UICollectionView 中加载所有相机胶卷视频及其持续时间(同样,视频显示在相机胶卷中)。怎么做? 我检查了this 链接。但他们像图片一样展示他们的视频。
【问题讨论】:
标签: ios swift video uicollectionview
我想在 UICollectionView 中加载所有相机胶卷视频及其持续时间(同样,视频显示在相机胶卷中)。怎么做? 我检查了this 链接。但他们像图片一样展示他们的视频。
【问题讨论】:
标签: ios swift video uicollectionview
// 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
}
【讨论】:
我推荐使用苹果在 Xcode 中预置的照片框架。这个问题的答案已经在这里填写:
swift: How to load photos from photo library without using UIImagePickerController?
苹果自创的照片框架:
要添加视频,您可以使用 UIImagePickerCrollerSourceType:
picker.sourceType = UIImagePickerControllerSourceType.Camera
【讨论】: