Creating thumbnail from local video in swift
BaseZen's answer translated to Swift 2:
import UIKit
import AVFoundation
do {
let asset = AVURLAsset(URL: NSURL(fileURLWithPath: "/that/long/path"), options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImageAtTime(CMTimeMake(0, 1), actualTime: nil)
let uiImage = UIImage(CGImage: cgImage)
let imageView = UIImageView(image: uiImage)
// lay out this image view, or if it already exists, set its image property to uiImage
} catch let error as NSError {
print("Error generating thumbnail: \(error)")
}
Translated with some edits from:
First frame of a video using AVFoundation
var err: NSError? = nil
let asset = AVURLAsset(URL: NSURL(fileURLWithPath: "/that/long/path"), options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
let cgImage = imgGenerator.copyCGImageAtTime(CMTimeMake(0, 1), actualTime: nil, error: &err)
// !! check the error before proceeding
let uiImage = UIImage(CGImage: cgImage)
let imageView = UIImageView(image: uiImage)
// lay out this image view, or if it already exists, set its image property to uiImage
This is a cleaned up version of David's answer and tested against iOS 11 / Swift 4.x.
Please note the different calls for handling the initial time based on which version of Swift you are using.
func generateThumbnail(url: URL) -> UIImage? {
do {
let asset = AVURLAsset(url: url)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
// Select the right one based on which version you are using
// Swift 4.2
let cgImage = try imageGenerator.copyCGImage(at: .zero,
actualTime: nil)
// Swift 4.0
let cgImage = try imageGenerator.copyCGImage(at: kCMTimeZero,
actualTime: nil)
return UIImage(cgImage: cgImage)
} catch {
print(error.localizedDescription)
return nil
}
}
- Creates an
AVURLAsset
from the providedURL
- Creates an
AVAssetImageGenerator
using the newly createdAVURLAsset
, is responsible for making the thumbnail - appliesPreferredTrackTransform informs the generator to apply the matrix to the thumbnail generation. The default value is false.
- Attempts to create a
CGImage
at the first frame of the video track - Creates a
UIImage
using the newly created CGImage & returns it - If the image generation step fails, an error is caught and presented to the console along with a nil UIImage
BaseZen's answer translated Swift 3 / Swift 4
You need to set the location of the video you want to make a thumbnail of as the url asset path, like:
Don't forget to import AVFoundation
func generateThumbnail(path: URL) -> UIImage? {
do {
let asset = AVURLAsset(url: path, options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
return thumbnail
} catch let error {
print("*** Error generating thumbnail: \(error.localizedDescription)")
return nil
}
}
For everyone having issues with this I have created the following drop in example hosted on Github