1

我想从视频中获取缩略图,表视图滚动时从视频中生成图像时tableview重用单元格我曾尝试过到目前为止,此代码从视频获取视频缩略图需要时间和tableview从视频制作新帧时的滚动视图

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 


          UIActivityIndicatorView *indicatorG = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

          dispatch_async(dispatch_get_main_queue(), ^{ 
           [indicatorG removeFromSuperview]; 
           [indicatorG startAnimating]; 
           [indicatorG setCenter:Cell.thumbnail.center]; 
           [Cell.thumbnail addSubview:indicatorG]; 
          }); 

          AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil]; 
          AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
          generator.appliesPreferredTrackTransform=TRUE; 
          float getDuration = CMTimeGetSeconds(asset.duration); 
          CMTime thumbTime = CMTimeMakeWithSeconds(0,1); 

          AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){ 

           if (result != AVAssetImageGeneratorSucceeded) { 

            NSLog(@"couldn't generate thumbnail, error:%@", error); 

           } 
           else{ 

            UIImageView *imgV; 

             imgV = [[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:im]]; 


            dispatch_async(dispatch_get_main_queue(), ^{ 


              [indicatorG stopAnimating]; 
              Cell.thumbnail.image = imgV.image; 

            }); 
           } 
           //  [button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal]; 
          }; 

          CGSize maxSize = CGSizeMake(320, 180); 
          generator.maximumSize = maxSize; 
          [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler]; 

         }); 

但使用调度异步解决的tableview滚动的问题,新的问题是被干扰与此代码的缩略图的顺序,有人请帮助?

编辑

地址代码:

NSString *strUrl = [NSString stringWithFormat:@"%@",url]; 
NSURL *url = [NSURL URLWithString:strUrl]; 
+0

让块保留单元格的indexPath。这样你的订单就不会搞砸了。 – Mercurial

+0

@Mercurial我是iOS新手,你能详细解释一下吗? – John

+0

你的网址对于每个表格行是不同的? – 3stud1ant3

回答

0

如果此代码在运行cellForItemAtIndexPath不断滚动时表视图,这不是一个很好的实施。在cellForItemAtIndexPath中调用从视频生成图像的代价非常高昂。

不是在cellForItemAtIndexPath中准备缩略图,而是在将数据加载到表视图之前准备并存储缩略图。或者至少将缩略图存储在某处,然后在滚动以供重用时在cellForItemAtIndexPath中处理。下次在cellForItemAtIndexPath中使用存储的缩略图并始终更新单元格的图像。

+0

想法很好,我以同样的方式考虑过,但问题是每个缩略图都需要1秒钟才能生成,具体取决于互联网连接,我也试过实现,但如果tableview有太多的视频帖子,它会花了那么多秒钟,我意识到在cellforrowatindexpath中实现缩略图不是个好主意,但如果我们在后台线程中同时生成缩略图异步,只需要几秒钟就可以生成许多缩略图。 – John