Video Thumbnail Taking 10-15 Secs to Display

Video thumbnail taking 10-15 secs to display

The problem is that you are calling thumbnailForVideoAtURL on a background thread. You need to be on the main thread because you are talking to the interface.

tempDkAsset.fetchAVAssetWithCompleteBlock { (tempVideo, info) in
DispatchQueue.main.async {
tempImageView.image = self.thumbnailForVideoAtURL(tempVideo!)
}
}

Creating thumbnail from local video in swift

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

How set thumbnails image of video player

For Swift 2, to create an image out of video use this code:

    var thumbImage: UIImage?
let fileURL = NSURL(fileURLWithPath: filePath)
let asset = AVAsset(URL: fileURL)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)
if let cgImage = try? assetImgGenerate.copyCGImageAtTime(time, actualTime: nil) {
thumbnailImage = UIImage(CGImage: cgImage)
}

now you have image. To make it cover you must use MPNowPlayingInfo. Add this piece of code when you update info:

    // works to do to initialise now playing info
if let artwork = thumbImage {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: artwork)
} else {
nowPlayingInfo[MPMediaItemPropertyArtwork] = nil
}
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nowPlayingInfo

How to get last frame from video?

I have solved this , to get only last frame from video the code is following:

  let asset : AVURLAsset = AVURLAsset(URL: videoURL, options: nil)
let generate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
generate.appliesPreferredTrackTransform = true

var err : NSError? = nil
var lastFrameTime = Int64(CMTimeGetSeconds(asset.duration)*60.0)
let time : CMTime = CMTimeMake(lastFrameTime, 2)
let imgRef : CGImageRef = generate.copyCGImageAtTime(time, actualTime: nil, error: &err)
let img : UIImage = UIImage(CGImage: imgRef)!

This is how I get last image of video in img variable.



Related Topics



Leave a reply



Submit