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
How to Make a Rounded Oval Button
Expand Uitextview and Uitableview When Uitextview's Text Extends Beyond 1 Line
Custom Mkoverlayrenderer Drawmaprect Function Not Drawing Polygons
Programmatically Obtaining the Imei or Udid of an iOS Device
How to Use Tap Gesture in Accessibility in Swift
Save Struct in Class to Nsuserdefaults Using Swift
How to Use Urlsession with Proxy in Swift 3
Indexing into Array of Functions: Expression Resolves to an Unused L-Value
Format Currency in Textfield in Swift on Input
How to Convert Duration Form Youtube API in Swift
Uitapgesturerecognizer Called Immediately
Uialertcontroller Visible Only When Viewdidappear Has Finished Its Call
Swift:How to Find the Position(X,Y) of a Letter in a Uilabel
Getting Uitableview Error "Unable to Dequeue a Cell with Identifier Cell"