Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Photos
- import PhotosUI
- struct Video {
- private(set) var videoUrl: URL?
- private(set) var thumbnail: UIImage?
- private init () {}
- init(videoUrl: URL?) {
- self.videoUrl = videoUrl
- guard let url = videoUrl else { return }
- self.thumbnail = generateThumbnail(from: url)
- }
- }
- class ArtistProfileVC: HubbleBaseVC {
- lazy var allVideos: [Video] = [] {
- didSet {
- videoCollection.reloadData()
- }
- }
- }
- // MARK: - PHPickerViewControllerDelegate
- extension ArtistProfileVC: PHPickerViewControllerDelegate {
- // for opening video picker
- func openPHPicker(selectionLimit: Int, tag: Int) {
- var phpConfig = PHPickerConfiguration()
- phpConfig.selectionLimit = selectionLimit
- phpConfig.filter = .any(of: [.videos, .slomoVideos, .timelapseVideos])
- let phPickerVC = PHPickerViewController(configuration: phpConfig)
- phPickerVC.delegate = self
- self.navigationController?.present(phPickerVC, animated: true)
- }
- func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
- portfolioVideosSelected(results: results)
- }
- func portfolioVideosSelected(results: [PHPickerResult]) {
- self.dismiss(animated: true)
- results.forEach { result in
- result.itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier) { url, error in
- guard let url = url, error == nil else { return }
- dump(url.absoluteURL)
- DispatchQueue.main.async {
- self.allVideos.append(.init(videoUrl: url))
- }
- }
- }
- }
- }
- //-----------------------------------------------------------------------------------------
- import AVFoundation
- import UIKit
- // MARK: - Using this for Generating Thumbnail.
- func generateThumbnail(from videoUrl: URL) -> UIImage? {
- let asset = AVAsset(url: videoUrl)
- let imageGenerator = AVAssetImageGenerator(asset: asset)
- imageGenerator.appliesPreferredTrackTransform = true
- let time = CMTime(seconds: 1, preferredTimescale: 1)
- do {
- let imageRef = try imageGenerator.copyCGImage(at: time, actualTime: nil)
- return UIImage(cgImage: imageRef)
- } catch {
- print(error.localizedDescription)
- return nil
- }
- }
- // --------------------------------------------------------------------------------------------------------
- For playing video am using AVKit
- func playVideo(url: URL) {
- let player = AVPlayer(url: url)
- let vc = AVPlayerViewController()
- vc.player = player
- self.present(vc, animated: true) {
- vc.player?.play()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement