playing video from UIView with YPImagePicker

Hello everyone,

I am having a little trouble and this might be simple ot solve. I am trying to display the video I have in one controller to the next. I imported all the files that I need.



let picker = YPImagePicker(configuration: config)

picker.didFinishPicking { [unowned picker] items, cancelled in

if cancelled {

picker.dismiss(animated: true, completion: nil)

} else if let photo = items.singlePhoto {

let vc = NewInstructionsController()

vc.selectedImage = photo.modifiedImage ?? photo.image

vc.stepCount = self.stepCount

picker.pushViewController(vc, animated: true)

} else if let video = items.singleVideo {

video.fetchData(completion: { (data) in

let vc = NewInstructionsController()

// vc.selectedImage = video.thumbnail

vc.selectedVideo = data

vc.stepCount = self.stepCount

picker.pushViewController(vc, animated: true)

})

} else {

picker.dismiss(animated: true, completion: nil)

}

}

present(picker, animated: true, completion: nil)

}


On my second view I am just trying to call the video to display. I can get images to work but I need videos to play in a view that is located in the same area.



var selectedVideo: Data?


let instructionsVideo: UIView = {

let iv = UIView()

iv.layer.cornerRadius = 3

iv.contentMode = .scaleAspectFill

iv.clipsToBounds = true

iv.layer.masksToBounds = true

return iv

}()


How would I get my video that I recorded from my imagepicker to the next controller and it will display in a view that I have over my uiimageview? I wondered if anyone can help me please? Thanks

Replies

You need to:


    import AVFoundation


And get an image from the video:

func videoSnapshot(videoUrl: URL) -> UIImage? {
        let asset = AVURLAsset(url: videoUrl)
        let generator = AVAssetImageGenerator(asset: asset)
        generator.appliesPreferredTrackTransform = true
      
        let timestamp = UIImage(seconds: 1, preferredTimescale: 60) // Time of the video for the image
      
        do {
            let imageRef = try generator.copyCGImage(at: timestamp, actualTime: nil)
            return UIImage(cgImage: imageRef, size:.zero) // The size of the new image. If size is NSZeroSize, the pixel dimensions of cgImage are assumed as the image’s size.
        }
        catch let error {
            print("Image generation failed with error \(error)")
            return nil
        }
    }

And display the image directly in the image view.


To play the video you need to use an AVPlayer.