Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a preview image from a video swift

Tags:

ios

swift

I have a folder in my documents directory containing images and videos, In my app i have a collection view that displays the images in each cell.

I have accomplished this with a load image function that returns a UIImage

    // Get the UI Image from the path passed in
    let image = UIImage(contentsOfFile: path)

    if image == nil
    {
        print("No Image at this path");
    }
    else
    {
        return image
    }

However I can't seem to find something equivalent for videos, Ideally I want it to show as the video player thing like in photo album where you see a still of the video with a little play button on it that when clicked plays the video.

Failing that purely being able to get a still from the video at the specified path I have to be returned as a UIImage I can display would be ok.

I am new to iOS stuff so just wondering if there is some equivalent type like UIImage for videos that means I can just load and display it on the collection view.

like image 809
AngryDuck Avatar asked Sep 20 '15 14:09

AngryDuck


4 Answers

You need a snapshot of the video and display it along with a play button. Here is my func in Swift 2 that can get you the video-snapshot for a video file at path filePathLocal:

func videoSnapshot(filePathLocal: String) -> UIImage? {

    let vidURL = URL(fileURLWithPath:filePathLocal as String)
    let asset = AVURLAsset(url: vidURL)
    let generator = AVAssetImageGenerator(asset: asset)
    generator.appliesPreferredTrackTransform = true

    let timestamp = CMTime(seconds: 1, 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
    }
}
like image 115
Nishant Avatar answered Nov 05 '22 19:11

Nishant


Swift 3 version

func videoPreviewUiimage(fileName:String) -> UIImage? {
    let filePath = NSString(string: "~/").expandingTildeInPath.appending("/Documents/").appending(fileName)

    let vidURL = NSURL(fileURLWithPath:filePath)
    let asset = AVURLAsset(url: vidURL as URL)
    let generator = AVAssetImageGenerator(asset: asset)
    generator.appliesPreferredTrackTransform = true

    let timestamp = CMTime(seconds: 2, 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
    }
}

Based on Nishant answer above.

like image 10
mourodrigo Avatar answered Nov 05 '22 19:11

mourodrigo


Using a URL for your path is better than using String:

func videoPreviewImage(url: URL) -> UIImage? {
    let asset = AVURLAsset(url: url)
    let generator = AVAssetImageGenerator(asset: asset)
    generator.appliesPreferredTrackTransform = true
    if let cgImage = try? generator.copyCGImage(at: CMTime(seconds: 2, preferredTimescale: 60), actualTime: nil) {
        return UIImage(cgImage: cgImage)
    }
    else {
        return nil
    }
}
like image 8
Aaron Halvorsen Avatar answered Nov 05 '22 19:11

Aaron Halvorsen


For swift 3.0

func generateThumbnailForVideoAtURL(filePathLocal: NSString) -> UIImage? {

let vidURL = NSURL(fileURLWithPath:filePathLocal as String)
    let asset = AVURLAsset(url: vidURL as URL)
    let generator = AVAssetImageGenerator(asset: asset)
    generator.appliesPreferredTrackTransform = true

let timestamp = CMTime(seconds: 1, 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
}

}
like image 7
Disha Avatar answered Nov 05 '22 19:11

Disha