如何从iOS中的ALAsset获取视频的缩略图?

时间:2015-07-07 07:42:41

标签: ios objective-c avplayer alassetslibrary cmtime

我希望从视频中获取每一帧的缩略图,然后将这些图像保存在可变图像阵列中。

我想用这些图像作为动画播放。

Width

我使用上面的代码来获取缩略图,但问题是如果有2秒的视频我只获得2个缩略图但我想要20个缩略图(每秒10个缩略图)。

那么,如何使用CMTimeMake获取每1秒的缩略图

4 个答案:

答案 0 :(得分:8)

代码表引用网站:Thumbnail image from Video

目标 - C

-(UIImage *)generateThumbImage : (NSString *)filepath
{
    NSURL *url = [NSURL fileURLWithPath:filepath];

    AVAsset *asset = [AVAsset assetWithURL:url];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
self.imageGenerator.appliesPreferredTrackTransform = YES; 
        CMTime time = [asset duration];
        time.value = 0;
        Float duration = CMTimeGetSeconds([myAsset duration]);
   for(Float i = 0.0; i<duration; i=i+0.1)
    {
     CGImageRef imgRef = [self.imageGenerator copyCGImageAtTime:CMTimeMake(i, duration) actualTime:NULL error:nil];
     UIImage* thumbnail = [[UIImage alloc] initWithCGImage:imgRef scale:UIViewContentModeScaleAspectFit orientation:UIImageOrientationUp];
    [thumbnailImages addObject:thumbnail];
    }
}

Swift

func generateThumbImage(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
    }

答案 1 :(得分:1)

@ Swirit Modi解决方案在Swift 3中有一些小改动:

func generateThumbImage(url : URL) -> UIImage?{

  let asset = AVAsset(url: url)
  let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
  let time = CMTimeMake(1, 30)
  let img = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil)

  guard let cgImage = img else { return nil }

  let frameImg    = UIImage(cgImage: cgImage)
  return frameImg
}

答案 2 :(得分:0)

以下代码从0.41

获取视频中的拇指图像
NSString *str = [[self.videoArray objectAtIndex:i] valueForKey:@"vName"];
NSURL *videoURL = [NSURL URLWithString:str] ;
MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] initWithContentURL:videoURL]autorelease];
UIImage  *thumbnail = [player thumbnailImageAtTime:0.41 timeOption:MPMovieTimeOptionNearestKeyFrame];
player = nil;

答案 3 :(得分:0)

extension UIImageView{
func getImageThumnail(forLink link: String) {
    let asset = AVAsset(url: URL(string: link)!)
    let assetImgGenerate = AVAssetImageGenerator(asset: asset)
    assetImgGenerate.appliesPreferredTrackTransform = true
    let time = CMTimeMakeWithSeconds(Float64(50), 100)
    DispatchQueue.global(qos: .userInteractive).async {
      do {
            let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
            let thumbnail = UIImage(cgImage: img)
            DispatchQueue.main.async {
                self.image = thumbnail
            }
    } catch {
    }
    }
}
}