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