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

是的显示,但现在主线程忙,所有的界面不可点击,直到加载图像。 –