1

我正在捕捉视频它工作正常,但我想获取该视频的缩略图并在 ImageView 中显示它任何想法如何在下面得到这个是我的代码。

if ([type isEqualToString:(NSString *)kUTTypeVideo] || [type isEqualToString:(NSString *)kUTTypeMovie])
{       
    NSURL*videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    NSLog(@"found a video");

    videoData = [[NSData dataWithContentsOfURL:videoURL] retain];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormat setDateFormat:@"dd-MM-yyyy_HH:mm:SS"];
    NSDate *now = [[[NSDate alloc] init] autorelease];
    NSDate* theDate = [dateFormat stringFromDate:now];

    NSString*myDate=[dateFormat stringFromDate:theDate];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];        

    NSString*test=@"test";      

    NSString*testUser=[test stringByReplacingOccurrencesOfString:@" " withString:@""];  

    videopath= [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mov",documentsDirectory,testUser]] autorelease];        


    BOOL success = [videoData writeToFile:videopath atomically:NO];     


    NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
    NSLog(@"video path --> %@",videopath);


    NSURL *movieURL = [NSURL fileURLWithPath:videopath];
    AVURLAsset *avUrl = [AVURLAsset assetWithURL:movieURL];
    CMTime time = [avUrl duration];
    int seconds = ceil(time.value/time.timescale);

//  durationTime=[NSString stringWithFormat:@"%d",seconds];

//  insertTime=[NSString stringWithFormat:@"%d",seconds];      


    NSString*messageA=[NSString stringWithFormat:@"You have recorded video of duration of %d seconds  ",seconds];       

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:messageA 
                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];


    NSDate* date = [NSDate date];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];
    //[formatter setDateFormat:@"MM-dd-yyyy"];

    NSString* str = [formatter stringFromDate:date];
4

3 回答 3

3

试试这个:

 #import<AssetsLibrary/AssetsLibrary.h>
 #import<AVFoundation/AVFoundation.h>
 #import <AudioToolbox/AudioToolbox.h>
 #import <CoreMedia/CoreMedia.h>
 #import <MediaPlayer/MediaPlayer.h>

 //create thumbnail from video


  AVAsset *asset = [AVAsset assetWithURL:url];// url= give your url video here
  AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    CMTime time = CMTimeMake(1, 5);//it will create the thumbnail after the 5 sec of video 
     CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
   UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
 cell.backGround.image=thumbnail;// whichever imageview you want give this image

它会帮助你。

于 2013-10-05T06:24:54.390 回答
0

添加 MPMoviePlayerController(见下文)是一个更快的选项。上面的代码需要 5-10 秒来生成一个缩略图,你也可以在 UIImageview 中显示

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];
于 2013-10-05T06:32:18.273 回答
-1
#import <AVFoundation/AVFoundation.h>

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

    AVAsset *asset = [AVAsset assetWithURL:url];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    CMTime time = [asset duration];
    time.value = 1000; //Time in milliseconds
    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;
}
于 2014-12-04T23:10:18.857 回答