如何在IOS中为UITableView单元显示.mov的缩略图

时间:2014-02-04 10:02:07

标签: ios iphone

嗨,我对ios非常新,我已经阅读了初学者书籍和中级书籍,我理解ios,有些事我无法完成。即时尝试将.mov视频的缩略图放入像youtube应用程序这样的单元格中,我想出如何将缩略图放入UIImageView

NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"e.mov" ofType:nil];
NSURL *videoURl = [[NSURL alloc ] initFileURLWithPath:videoPath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURl options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generate.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(0.0, 600);
CMTime actualTime;

CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:&actualTime error:&err];

UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];
[self.myImageView setImage:img];

这就是我在标准UIViewController上将缩略图映射到UIImageView所做的,我只是想弄清楚如何在UITableViewCell *单元格中进行操作;

我有18个视频我希望以缩略图的形式显示在单元格中,一旦触摸到单元格,就会激活方法:方法被激活然后进入detailViewCOntroller

NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"e.mov" ofType:nil];

如何将其设为NSMutableArray并将其放入

NSURL *videoURl = [[NSURL alloc ] initFileURLWithPath:videoPath];

并使其在单元格中工作

基本上我无法弄清楚如何在单元格中以缩略图的形式显示视频数组 请帮忙

2 个答案:

答案 0 :(得分:0)

试试这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath] ;

   AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoPath options:nil];
   AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
   CMTime time = CMTimeMake(1, 30);
   CGImageRef thumbImg = [generate copyCGImageAtTime:time actualTime:NULL error:&err];


   if (cell == nil)
   {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   }
   cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
   cell.imageView.image = [UIImage imageWithCGImage:thumbImg];
   return cell;
}

答案 1 :(得分:0)

这是...... get screenshot first frame of the video

- (UIImage*)loadImage {

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil];
    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    NSError *err = NULL;
    CMTime time = CMTimeMake(1, 60);
    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
    NSLog(@"err==%@, imageRef==%@", err, imgRef);

    return [[UIImage alloc] initWithCGImage:imgRef];

}