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

烈酒焚心 提交于 2019-12-21 17:13:58

问题


I want to get thumbnail images of every frame from video and then save this images in Mutable Array of images.

I want to use this images to play as a animation.

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];
}

I am using above code to get thumbnail images but the problem is if there is a 2 seconds video i am only getting 2 thumbnails but i want 20 thumbnails (10 thumbnail per second).

So, how to use CMTimeMake to get thumbnails for every .1 second


回答1:


Code form reference site : 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
    }



回答2:


@Kirit Modi solution in Swift 3 with some small changes:

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
}



回答3:


Following code for the getting the thumb image from video at 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;



回答4:


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


来源:https://stackoverflow.com/questions/31262606/how-to-get-thumbnail-image-of-video-from-alasset-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!