3

我在使用(Grand Central Dispatch)在另一個線程中使用MPMoviePlayer時遇到了問題。我想通過網址吸引視頻,剪切第5幀並返回圖片。在模擬器工作正常,但在一個真正的設備 - 秋天。這裏有什麼問題?也許它只適用於主線程?從線程中的視頻url中提取縮略圖

NSURL* url = [NSURL URLWithString:filePath]; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void) 
{ 
    MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
    moviePlayer.shouldAutoplay = NO; 
    UIImage* thumbnail = [moviePlayer thumbnailImageAtTime:5 timeOption:MPMovieTimeOptionNearestKeyFrame]; 

    dispatch_async(dispatch_get_main_queue(), ^(void) 
    { 
    [[Singleton sharedSingleton].imageCache setValue:thumbnail forKey:filePath]; 
    [cell.presentationViewOutlet setImage:thumbnail];; 
    }); 
}); 

現在,我嘗試使用另一種方法,但它也適用於模擬器,並且在真實設備上不顯示。有時會返回imageRef = (null),有時會返回imageRef = <CGImage 0x1766dd20>

編輯:

NSURL* url = [NSURL URLWithString:filePath]; 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void) 
{ 
    AVAsset *asset = [AVAsset assetWithURL:videoURL]; 
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; 
    imageGenerator.appliesPreferredTrackTransform = TRUE; 
    CMTime time = CMTimeMakeWithSeconds(1, 1); 
    NSError *err = NULL; 
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:&err]; 
    dispatch_async(dispatch_get_main_queue(), ^(void) 
    { 
     NSLog(@"imageRef = %@", imageRef); 
     UIImage* image = [UIImage imageWithCGImage:imageRef]; 
     if (image != nil) 
     { 
      NSLog(@"image != nil"); 
      [view setImage:image]; 
      NSString* videoURLString = [videoURL absoluteString]; 
      [[Singleton sharedSingleton].imageCache setValue:image forKey:videoURLString]; 
      CGImageRelease(imageRef); 
     } 
     else 
     { 
      NSLog(@"err = %@", err); 
     } 
    }); 
}); 

錯誤:

如果imageRef =(空)表示ERR = Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x156c1a50 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x155b29a0 "The operation couldn’t be completed. (OSStatus error -12792.)", NSLocalizedFailureReason=An unknown error occurred (-12792)}

+1

您是否找到解決此問題的解決方案?我現在也面臨同樣的問題:( –

+0

@ VishnuKumar.S你找到了解決方案?面臨同樣的問題:(http://stackoverflow.com/questions/37374008/ios-swift-video-thumbnail-error – Sam

回答

1

一個的MPMoviePlayerController是用戶接口控制器。因此,它幾乎肯定不是線程安全的。文件沒有具體說明,但我敢打賭,它不是。

+0

是,你是對的,這是不可能的 –

+1

現在我試圖使用AVFoundation,但仍然有問題... –

+0

什麼樣的問題? – box86rowh

0

如果您移動異步部分內的imageref,它會更好嗎?像:

NSURL* url = [NSURL URLWithString:filePath]; 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void) 
{ 
    AVAsset *asset = [AVAsset assetWithURL:videoURL]; 
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; 
    imageGenerator.appliesPreferredTrackTransform = TRUE; 
    CMTime time = CMTimeMakeWithSeconds(1, 1); 
    dispatch_async(dispatch_get_main_queue(), ^(void) 
    { 
     NSError *err = NULL; 
     CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:&err]; 
     NSLog(@"imageRef = %@", imageRef); 
     UIImage* image = [UIImage imageWithCGImage:imageRef]; 
     if (image != nil) 
     { 
      NSLog(@"image != nil"); 
      [view setImage:image]; 
      NSString* videoURLString = [videoURL absoluteString]; 
      [[Singleton sharedSingleton].imageCache setValue:image forKey:videoURLString]; 
      CGImageRelease(imageRef); 
     } 
     else 
     { 
      NSLog(@"err = %@", err); 
     } 
    }); 
}); 
+1

是的顯示,但現在主線程忙,所有的界面不可點擊,直到加載圖像。 –