2012-09-26 62 views
3

我將捕獲的視頻保存在文檔目錄中,如下所示,現在我想顯示縮略圖並播放相同的視頻。如何操作?如何獲取保存在文檔目錄中的視頻的縮略圖

- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info 

{  
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType]; 

    [self dismissModalViewControllerAnimated:NO]; 

// Handle a movie capture 
if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) 
{ 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    NSString *movieName = [NSString stringWithFormat:@"%@.mov",[CoreDataFunctions getNameForVideoForDate:[CalendarFunctions getCurrentDateString]]]; 
    NSString *moviePath = [documentsDir stringByAppendingPathComponent:movieName]; 
    NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL]; 
    NSData * movieData = [NSData dataWithContentsOfURL:movieURL]; 
    NSLog(@"%@",moviePath); 
    if([movieData writeToFile:moviePath atomically:NO]) 
    { 
     if(![CoreDataFunctions saveVideoInfoInDatabaseForDate:[CalendarFunctions getCurrentDateString]]) 
     { 
      NSLog(@"Video was saved in doucment directory but could not be saved in core data"); 
     } 
    } 
    else 
    { 
     NSLog(@"Video could not be saved to the document directry"); 
    } 
} 
} 

回答

7

從文檔目錄獲取視頻這樣

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSError * error; 
NSArray *directoryContents = [[NSFileManager defaultManager] 
          contentsOfDirectoryAtPath:documentsDirectory error:&error]; 

//Take all images in NSMutableArray 
NSMutableArray *arrVideoImages = [[NSMutableArray alloc]init]; 
for(NSString *strFile in directoryContents) 
{ 
    NSString *strVideoPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,strFile]; 
    UIImage *img = [self getThumbNail:strVideoPath]; 
    [arrVideoImages addObject:img]; 

} 

而且添加這個方法:

-(UIImage *)getThumbNail:(NSString)stringPath 
{ 
    NSURL *videoURL = [NSURL fileURLWithPath:stringPath]; 

    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL]; 

    UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; 

    //Player autoplays audio on init 
    [player stop]; 
    [player release]; 
    return thumbnail; 
} 
+0

這工作完全fine.Thanx很多... –

相關問題