Getting a thumbnail from a video url or data in iP

2019-01-01 10:31发布

I am trying to acquire a thumbnail (of the first frame) from a video taken from iphone 3GS camera so i can display it. Anyone been successful at doing this? If so, how to do?

10条回答
何处买醉
2楼-- · 2019-01-01 10:37
-(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;
}

you can get the frame using the time.value suppose you want to 1 second frame then use the

time.value = 1000 //Time in milliseconds
查看更多
几人难应
3楼-- · 2019-01-01 10:37
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];

Check this link for an alternative: thumbnailImageAtTime: now deprecated - What's the alternative?

查看更多
像晚风撩人
4楼-- · 2019-01-01 10:43

Swift 2.1 Getting thumbnails at required time intervals

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
}
查看更多
伤终究还是伤i
5楼-- · 2019-01-01 10:45

For Swift 3.0

func createThumbnailOfVideoFromFileURL(_ strVideoURL: String) -> UIImage?{

let asset = AVAsset(url: URL(string: strVideoURL)!)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(Float64(1), 100)
do {
  let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
  let thumbnail = UIImage(cgImage: img)
  return thumbnail
} catch {
  /* error handling here */
}
return nil   }
查看更多
孤独总比滥情好
6楼-- · 2019-01-01 10:49

You will get Thumbnail Image From URL when you change "fileURLWithPath" to "URLWithString".

In My case it worked like this.

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;
查看更多
美炸的是我
7楼-- · 2019-01-01 10:51

The answer to this question is that one can now with 4.0 iOS get thumbnails using AVFoundation, the following code where the class property url is the movie url, will do the trick (you can get the thumbnail at any time, in the example its at time 0)

-(void)generateImage
{
    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform=TRUE;
    [asset release];
    CMTime thumbTime = CMTimeMakeWithSeconds(0,30);

    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
        if (result != AVAssetImageGeneratorSucceeded) {
            NSLog(@"couldn't generate thumbnail, error:%@", error);
        }
        [button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
        thumbImg=[[UIImage imageWithCGImage:im] retain];
        [generator release];
    };

    CGSize maxSize = CGSizeMake(320, 180);
    generator.maximumSize = maxSize;
    [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];

}
查看更多
登录 后发表回答