如何获取保存在文档目录中的视频缩略图

Posted

技术标签:

【中文标题】如何获取保存在文档目录中的视频缩略图【英文标题】:How to get thumbnails of a video saved in document directory 【发布时间】:2012-09-26 12:48:10 【问题描述】:

我将捕获的视频保存在如下所示的文档目录中,现在我想显示缩略图并播放相同的视频。如何做到这一点?

- (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");
    


【问题讨论】:

【参考方案1】:

像这样从 doc dir 获取视频

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;
 

【讨论】:

以上是关于如何获取保存在文档目录中的视频缩略图的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UIImage 保存到文档目录?

目录中的视频缩略图

Android:如何获取应用程序私有文件的视频缩略图?

如何在颤动中获取临时存储目录?

如何从 Flutter Web 中的 Uint8List 获取视频缩略图?

从照片中提取后,视频及其缩略图未按顺序保存