Advertisement
Wolverine_X-Man

Untitled

Jun 6th, 2023 (edited)
1,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.68 KB | Source Code | 0 0
  1. import UIKit
  2. import Photos
  3. import PhotosUI
  4.  
  5. struct Video {
  6.     private(set) var videoUrl: URL?
  7.     private(set) var thumbnail: UIImage?
  8.     private init () {}
  9.     init(videoUrl: URL?) {
  10.         self.videoUrl = videoUrl
  11.         guard let url = videoUrl else { return }
  12.         self.thumbnail = generateThumbnail(from: url)
  13.     }
  14. }
  15.  
  16. class ArtistProfileVC: HubbleBaseVC {
  17.     lazy var allVideos: [Video] = [] {
  18.         didSet {
  19.             videoCollection.reloadData()
  20.         }
  21.     }
  22. }
  23.  
  24. // MARK: - PHPickerViewControllerDelegate
  25. extension ArtistProfileVC: PHPickerViewControllerDelegate {
  26. // for opening video picker
  27. func openPHPicker(selectionLimit: Int, tag: Int) {
  28.         var phpConfig = PHPickerConfiguration()
  29.         phpConfig.selectionLimit = selectionLimit
  30.         phpConfig.filter = .any(of: [.videos, .slomoVideos, .timelapseVideos])
  31.         let phPickerVC = PHPickerViewController(configuration: phpConfig)
  32.         phPickerVC.delegate = self
  33.         self.navigationController?.present(phPickerVC, animated: true)
  34.     }
  35.  
  36.     func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
  37.         portfolioVideosSelected(results: results)
  38.     }
  39.    
  40.     func portfolioVideosSelected(results: [PHPickerResult]) {
  41.         self.dismiss(animated: true)
  42.         results.forEach { result in
  43.             result.itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier) { url, error in
  44.                 guard let url = url, error == nil else { return }
  45.                 dump(url.absoluteURL)
  46.                 DispatchQueue.main.async {
  47.                     self.allVideos.append(.init(videoUrl: url))
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54. //-----------------------------------------------------------------------------------------
  55. import AVFoundation
  56. import UIKit
  57. // MARK: - Using this for Generating Thumbnail.
  58. func generateThumbnail(from videoUrl: URL) -> UIImage? {
  59.     let asset = AVAsset(url: videoUrl)
  60.     let imageGenerator = AVAssetImageGenerator(asset: asset)
  61.     imageGenerator.appliesPreferredTrackTransform = true
  62.     let time = CMTime(seconds: 1, preferredTimescale: 1)
  63.     do {
  64.         let imageRef = try imageGenerator.copyCGImage(at: time, actualTime: nil)
  65.         return UIImage(cgImage: imageRef)
  66.     } catch {
  67.         print(error.localizedDescription)
  68.         return nil
  69.     }
  70. }
  71.  
  72. // --------------------------------------------------------------------------------------------------------
  73.  
  74. For playing video am using AVKit
  75.  
  76. func playVideo(url: URL) {
  77.     let player = AVPlayer(url: url)
  78.     let vc = AVPlayerViewController()
  79.     vc.player = player
  80.     self.present(vc, animated: true) {
  81.         vc.player?.play()
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement