Getting thumbnail from movie

2020-06-28 09:03发布

I have an app where I can shoot a video and then store it in core-data with some other data. It's stored as Transformable and "Store in External Record File". I get the video clip into an object called movie like this;

(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
     movie = [info objectForKey:UIImagePickerControllerEditedImage];

Where I get stuck is to get a thumbnail from the movie. Using MPMoviePlayerController I have to have an URL, but the movie isn't stored anywhere yet. Plus finding the URL from the core-data is a mystery as well.

The closest help I can find here is Getting a thumbnail from a video url or data in iPhone SDK . But I get caught out on the URL issue.

I am saving it to core-data like this;

     NSManagedObjectContext *context = [self managedObjectContext];
     NSManagedObject *newMedia = [NSEntityDescriptioninsertNewObjectForEntityForName:@"Media" inManagedObjectContext:context];
     [newMedia setValue:@"Video Clip " forKey:@"title"];
     [newMedia setValue:now forKey:@"date_time"];
     [newMedia setValue:movie forKey:@"movie"];
     [newMedia setValue:[self generateImage] forKey:@"frame"];

I would be grateful if there is someone out there that can give me a pointer.

标签: ios video
3条回答
我欲成王,谁敢阻挡
2楼-- · 2020-06-28 09:30

You can get your movie URL like this and then use this URL to get thumbnail using MPMoviePlayerController.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
     . .. .
     .. .. 
 }
查看更多
女痞
3楼-- · 2020-06-28 09:49

To get thumbnail from movie...

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if ([[info objectForKey:@"UIImagePickerControllerMediaType"] rangeOfString:@"movie"].location!=NSNotFound)
 {
    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:[info objectForKey:@"UIImagePickerControllerMediaURL"]];
    theMovie.view.frame = self.view.bounds;
    theMovie.controlStyle = MPMovieControlStyleNone;
    theMovie.shouldAutoplay=NO;
    UIImage *imgTemp = [theMovie thumbnailImageAtTime:0 timeOption:MPMovieTimeOptionExact];
 }
}
查看更多
一夜七次
4楼-- · 2020-06-28 09:50

I used like this in my app

- (UIImage *)imageFromMovie:(NSURL *)movieURL atTime:(NSTimeInterval)time {
    // set up the movie player
    MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] 
                                   initWithContentURL:movieURL];
    mp.shouldAutoplay = NO;
    mp.initialPlaybackTime = time;
    mp.currentPlaybackTime = time;
    // get the thumbnail
    UIImage *thumbnail = [mp thumbnailImageAtTime:time 
                                       timeOption:MPMovieTimeOptionNearestKeyFrame];
    // clean up the movie player
    [mp stop];
    [mp release];
    return(thumbnail);
}

used like

imageView.image = [self imageFromMovie:fileURL atTime:10.0];
查看更多
登录 后发表回答