我正在尝试从iphone 3GS相机拍摄的视频中获取(第一帧的)缩略图,以便我可以显示它。怎么做?
答案 0 :(得分:69)
答案 1 :(得分:68)
-(UIImage *)generateThumbImage : (NSString *)filepath
{
NSURL *url = [NSURL fileURLWithPath:filepath];
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CMTime time = [asset duration];
time.value = 0;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); // CGImageRef won't be released by ARC
return thumbnail;
}
您可以使用time.value获取帧,假设您想要1秒帧然后使用
time.value = 1000 //Time in milliseconds
答案 2 :(得分:23)
NSURL *videoURL = [NSURL fileURLWithPath:url];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
//Player autoplays audio on init
[player stop];
[player release];
检查此链接以获取替代方案: thumbnailImageAtTime: now deprecated - What's the alternative?
答案 3 :(得分:13)
我找到的最佳方法...... MPMoviePlayerController thumbnailImageAtTime:timeOption
答案 4 :(得分:12)
你可以通过两种方式在swift中生成1. AVFoundation 2. MPMoviePlayerController
1.
func generateThumnail(url : NSURL) -> UIImage{
var asset : AVAsset = AVAsset.assetWithURL(url) as AVAsset
var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
var error : NSError? = nil
var time : CMTime = CMTimeMake(1, 30)
var img : CGImageRef = assetImgGenerate.copyCGImageAtTime(time, actualTime: nil, error: &error)
var frameImg : UIImage = UIImage(CGImage: img)!
return frameImg
}
2.
override func viewDidLoad() {
super.viewDidLoad()
var moviePlayer : MPMoviePlayerController! = MPMoviePlayerController(contentURL: moviePlayManager.movieURL)
moviePlayer.view.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width:
self.view.frame.size.width, height: self.view.frame.height)
moviePlayer.fullscreen = true
moviePlayer.controlStyle = MPMovieControlStyle.None
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "videoThumbnailIsAvailable:",
name: MPMoviePlayerThumbnailImageRequestDidFinishNotification,
object: nil)
let thumbnailTimes = 3.0
moviePlayer.requestThumbnailImagesAtTimes([thumbnailTimes],
timeOption: .NearestKeyFrame)
}
func videoThumbnailIsAvailable(notification: NSNotification){
if let player = moviePlayer{
let thumbnail =
notification.userInfo![MPMoviePlayerThumbnailImageKey] as? UIImage
if let image = thumbnail{
/* We got the thumbnail image. You can now use it here */
println("Thumbnail image = \(image)")
}
}
答案 5 :(得分:8)
Swift 2代码:
func previewImageForLocalVideo(url:NSURL) -> UIImage?
{
let asset = AVAsset(URL: url)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
var time = asset.duration
//If possible - take not the first frame (it could be completely black or white on camara's videos)
time.value = min(time.value, 2)
do {
let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: nil)
return UIImage(CGImage: imageRef)
}
catch let error as NSError
{
print("Image generation failed with error \(error)")
return nil
}
}
答案 6 :(得分:8)
对于Swift 3.0
name
答案 7 :(得分:1)
也许这对面临同样问题的其他人有用。我需要一个简单的解决方案来创建图像,PDF和视频的缩略图。为了解决这个问题,我创建了以下库。
https://github.com/prine/ROThumbnailGenerator
使用非常简单:
var thumbnailImage = ROThumbnail.getThumbnail(url)
它内部有三种不同的实现,根据文件扩展名,它确实创建了缩略图。如果您需要另一个文件扩展名的缩略图创建者,则可以轻松添加自己的实现。
答案 8 :(得分:1)
Swift 2.1 以所需的时间间隔获取缩略图
func getPreviewImageForVideoAtURL(videoURL: NSURL, atInterval: Int) -> UIImage? {
print("Taking pic at \(atInterval) second")
let asset = AVAsset(URL: videoURL)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(Float64(atInterval), 100)
do {
let img = try assetImgGenerate.copyCGImageAtTime(time, actualTime: nil)
let frameImg = UIImage(CGImage: img)
return frameImg
} catch {
/* error handling here */
}
return nil
}
答案 9 :(得分:1)
对于Swift 5
import AVKit
代码类似于::
// Get Thumbnail Image from URL
fileprivate func getThumbnailFromUrl(_ url: String?, _ completion: @escaping ((_ image: UIImage?)->Void)) {
guard let url = URL(string: url ?? "") else { return }
DispatchQueue.main.async {
let asset = AVAsset(url: url)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(value: 2, timescale: 1)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
completion(thumbnail)
} catch {
print("Error :: ", error.localizedDescription)
completion(nil)
}
}
}
用法:: 拍摄一张图片
@IBOutlet weak var imgThumbnail: UIImageView!
然后在以URL“ String”作为参数的缩略图调用此方法之后
self.getThumbnailFromUrl(videoURL) { [weak self] (img) in
guard let _ = self else { return }
if let img = img {
self?.imgThumbnail.image = img
}
}
请尝试使用它,如果它已用完,请发表评论 谢谢
答案 10 :(得分:0)
将“fileURLWithPath”更改为“URLWithString”时,您将从URL获取缩略图图像。
在我的情况下,它就像这样。
NSURL *url = [NSURL URLWithString:filepath];
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = [asset duration];
time.value = 0;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return thumbnail;
答案 11 :(得分:0)
雨燕4
SELECT
DISTINCT(Name) as UniqueNames
FROM
tableName
使用方法:
func generateThumbnail(for asset:AVAsset) -> UIImage? {
let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(value: 1, timescale: 2)
let img = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil)
if img != nil {
let frameImg = UIImage(cgImage: img!)
return frameImg
}
return nil
}