'How To create custom video preview screen which should look like default

I want Image Picker's Preview For Video in My Custom Camera but Somehow It Is Not Possible To use That Video PreView, How Can i Create Custom Video Preview Which should Look Like Default or Like this

enter image description here.



Solution 1:[1]

Here I found Solution For It, I Create Whole Custom View Which Will Look Like Default or Like This

enter image description here.

** Create preview freamI with the help of CollectionView **

You Should import

import UIKit    
import AVKit
import MobileCoreServices

class PreViewController: UIViewController, UIImagePickerControllerDelegate & UINavigationControllerDelegate {

@IBOutlet weak var slider: UISlider!
@IBOutlet weak var clvView: UICollectionView!
@IBOutlet weak var btnUseVide: UIButton!
@IBOutlet weak var btnPlay: UIButton!
@IBOutlet weak var btnRetake: UIButton!
@IBOutlet weak var ControllView: UIView!
@IBOutlet weak var videoView: UIView!

var player: AVPlayer!
var playerLayer: AVPlayerLayer!
var videoURL: VideoUrl!
var isPlaying = Bool()
var img = UIImage()
var arrImg = [UIImage]()

var timer = Timer()

override func viewDidLoad() {
    super.viewDidLoad()
    let url:URL = (videoURL?.URL)!
    player = AVPlayer(url: url)
    playerLayer = AVPlayerLayer(player: player)
    playerLayer.videoGravity = .resize
    videoView.layer.addSublayer(playerLayer)
    setUI()
    slider.minimumValue = 0
    slider.maximumValue = videoURL.time as! Float
    slider.transform = slider.transform.scaledBy(x: 1, y: 3)
    slider.tintColor = .clear
}

func setUI(){
    ControllView.layer.opacity = 0.4
    ControllView.layer.backgroundColor = UIColor.black.cgColor
    btnRetake.setTitleColor(.white, for: .normal)
    btnPlay.setTitleColor(.white, for: .normal)
    btnUseVide.setTitleColor(.white, for: .normal)
    btnPlay.setTitle("", for: .normal)
    btnRetake.setTitle("Retake", for: .normal)
    btnUseVide.setTitle("UseVideo", for: .normal)
    AppUtility.lockOrientation(.portrait)
    clvView.delegate = self
    clvView.dataSource = self
    clvView.register(UINib(nibName: "imgPriviewCell", bundle: nil), forCellWithReuseIdentifier: "imgPriviewCell")
    getImeges()
    scheduledTimerWithTimeInterval()
}
func getImeges(){
    let timesec = videoURL.time as! Int
    var finalTime = Int()
    var snapTime = Double()
    if timesec < 10{
        finalTime = timesec * 3
        snapTime = 0.20
    }else if timesec < 60{
        finalTime = timesec * 2
        snapTime = 0.30
    }else{
        finalTime = timesec / 2
        snapTime = 1
    }
    
    for i in 1...finalTime{
        let im = videoSnapshot(filePathLocal: videoURL.URL, secound: snapTime)!
        arrImg.append(im)
        if timesec < 10{
            snapTime += 0.20
        }else if timesec < 60{
            snapTime += 0.30
        }else{
            snapTime += 2
        }
    }
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    playerLayer.frame = videoView.bounds
}

@IBAction func btnClickedRetake(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

@IBAction func btnClickPlay(_ sender: Any) {
    if isPlaying{
        player.pause()
        isPlaying = false
        btnPlay.setImage(UIImage(named: "Play"), for: .normal)
    }else{
        player.play()
        isPlaying = true
        btnPlay.setImage(UIImage(named: "pause"), for: .normal)
    }
}

@IBAction func btnClickUse(_ sender: Any) {
}

func videoSnapshot(filePathLocal: URL, secound: Double) -> UIImage? {
    let asset = AVURLAsset(url: filePathLocal)
    let generator = AVAssetImageGenerator(asset: asset)
    generator.appliesPreferredTrackTransform = true
    let timestamp = CMTime(seconds: secound, preferredTimescale: 60)
    do {
        let imageRef = try generator.copyCGImage(at: timestamp, actualTime: nil)
        return UIImage(cgImage: imageRef)
    }
    catch let error as NSError
    {
        print("Image generation failed with error \(error)")
        return nil
    }
}

@IBAction func sliderDidChnage(_ sender: UISlider) {
    player.pause()
    isPlaying = false
    btnPlay.setImage(UIImage(named: "Play"), for: .normal)
    var timeInSecond = slider.value
    timeInSecond *= 1000
    let cmTime = CMTimeMake(value: Int64(timeInSecond), timescale: 1000)
    player.seek(to: cmTime, toleranceBefore: .zero, toleranceAfter: .zero)
}

func scheduledTimerWithTimeInterval(){
    timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(updateCounting), userInfo: nil, repeats: true)
}

@objc func updateCounting(){
    let currentTime:Double = player.currentItem!.currentTime().seconds
    slider.value = Float(currentTime)
}}

extension PreViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return arrImg.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = clvView.dequeueReusableCell(withReuseIdentifier: "imgPriviewCell", for: indexPath) as! imgPriviewCell
    cell.imgView.image = arrImg[indexPath.row]
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let numberOfCell = arrImg.count
    let cellHeight = clvView.frame.size.height
    let cellWidth = (clvView.frame.size.width) / CGFloat(numberOfCell)
    let flow = UICollectionViewFlowLayout()
    flow.itemSize = CGSize(width: cellWidth, height: cellHeight)
    flow.scrollDirection = .horizontal
    flow.minimumInteritemSpacing = 0
    flow.minimumLineSpacing = 0
    return CGSize(width: cellWidth, height: cellHeight)
}}

---> You Can Find All The Method Which I Used For Create This View,

---> I am Getting My Video Url From videoUrl Class so You Can Set Your Own Way To get Url

---> In getImeges Method You Can See I Take Snap Shot Of video From Various Duration Of Video.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1