我想加載UICollectionView中的所有相機膠捲視頻及其持續時間(同樣的方式,視頻以相機膠捲顯示)。如何做? 我檢查了this鏈接。但他們展示了他們的視頻,如圖像。Swift - 如何從相機膠捲獲取視頻並將其顯示在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
}
- 注:隱私 - 圖片庫使用情況說明 - >我需要你的照片庫中添加此項到Info.plist文件
你能上傳全班嗎? @Yogesh Makwana –
我沒有得到你的代碼的第二部分...在哪裏寫和函數** getAssetFromPhoto()**,我應該在viewDidLoad()方法中調用它? –
yes getAssetFromPhoto()調用viewDidLoad()並顯示視頻thumnail:func collectionView(_ collectionView:UICollectionView,cellForItemAt indexPath:IndexPath) - > UICollectionViewCell { } –
我推薦使用蘋果已經預置到Xcode中的照片框架。在這個問題的答案已經被填寫在這裏:
要添加視頻,你可以使用一個UIImagePickerCrollerSourceType:
picker.sourceType = UIImagePickerControllerSourceType.Camera
swift: How to load photos from photo library without using UIImagePickerController?
的照片框架蘋果公司創建
視頻看起來像圖像......那裏沒有持續時間。 –
你想顯示視頻,而不是圖像,對不對? –
是的...我只是想加載視頻 –
- 1. iOS Swift 2 - 獲取相機膠捲中的視頻路徑
- 2. Swift - 如何獲取相機膠捲中所有視頻文件的網址?
- 3. 如何將視頻保存到相機膠捲並獲取資產URL?
- 4. 如何使用AVFoundation將視頻寫入iPhone相機膠捲?
- 5. 如何將錄製的視頻保存到相機膠捲?
- 6. 從相機膠捲中選擇的視頻的URL
- 7. 刷新顯示相機膠捲內容
- 8. 使用從AVRURLAsset從相機膠捲保存的視頻 - initWithURL - iPhone
- 9. 如何將視頻從應用程序文檔目錄移至相機膠捲?
- 10. 如何從相機膠捲獲取照片?
- 11. 在iOS相機膠捲中識別視頻的技術
- 12. 從SpriteKit相機膠捲播放視頻SKVideoNode
- 13. 在WP7中顯示相機膠捲和相冊圖像
- 14. 如何從相機意圖獲取視頻並將其保存到目錄?
- 15. 如何將視頻導入iOS 4.2中的iPhone模擬器相機膠捲?
- 16. 如何將相機膠捲中的所有照片和視頻插入陣列?
- 17. 如何將資源中存儲的視頻保存到相機膠捲?
- 18. 如何使用應用內按鈕將ReplayKit視頻保存到相機膠捲
- 19. 反應原生 - 如何獲得相機膠捲視頻的縮略圖uri
- 20. 獲取相機膠捲圖像及其EXIF數據?
- 21. 將視頻放入相機膠捲中的一個特定相冊
- 22. 如何將相機膠捲中的視頻添加到Xcode上的應用程序(Swift)
- 23. 如何將UIImageView與子視圖保存到相機膠捲?
- 24. 如何從相機中抓取YUV格式的視頻,顯示並處理它
- 25. xcode在OS X中顯示iPad相機膠捲
- 26. 如何從windows phone 8.1中的相機膠捲獲取存儲文件
- 27. AVAssetExportSession - 導出的視頻不兼容相機膠捲
- 28. 無法保存本地視頻文件到相機膠捲
- 29. 在Android上從相機膠捲中提取圖像數據?
- 30. 如何從相機膠捲可視化(而不是選取)圖像
的可能的複製:([SWIFT如何加載從照片庫中的照片,而無需使用的UIImagePickerController?] http://stackoverflow.com/questions/35115117/swift-how-to-load-photos-from-photo-library-without-using-uiimagepickercontroll) –