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

How to get thumbnail image of video from ALAsset in iOS?

我想从视频中获取每一帧的缩略图,然后将这些图像保存在可变图像数组中。

我想用这个图片作为动画播放。

NSURL* assetURL = [self.asset valueForProperty:ALAssetPropertyAssetURL];
NSDictionary* assetOptions = nil;

AVAsset* myAsset = [[AVURLAsset alloc] initWithURL:assetURL options:assetOptions];

self.imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:myAsset];


int duration = CMTimeGetSeconds([myAsset duration]);

for(int i = 0; i<duration; i++)
{
    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];
}

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

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

以下代码用于从 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;

代码形式参考站点:Thumbnail image from Video

Objective - 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
    }

@Kirit 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
}
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 {
    }
    }
}
}