【问题标题】:Extract thumbnail from video url in threads从线程中的视频网址中提取缩略图
【发布时间】:2014-01-10 21:11:35
【问题描述】:

我在另一个线程中的 MPMoviePlayer 出现问题,使用 (Grand Central Dispatch)。我想通过 url 吸引视频,剪切第 5 帧并返回图片。在模拟器中工作正常,但在真实设备上 - 下降。这里有什么问题?也许它只适用于主线程?

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 = (null) 显示 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)}

【问题讨论】:

标签: ios objective-c asynchronous avfoundation mpmovieplayercontroller


【解决方案1】:

MPMoviePlayerController 是一个用户界面控制器。因此,几乎可以肯定它不是线程安全的。文档没有具体说明,但我敢打赌它不是。

【讨论】:

  • 什么样的问题?
  • 不可预测,有时返回 imageRef = (null),有时返回 imageRef =
  • If imageRef = (null) show err = Error Domain=AVFoundationErrorDomain Code=-11800 "操作无法完成" UserInfo=0x156c1a50 {NSLocalizedDescription=操作无法完成,NSUnderlyingError=0x155b29a0 "操作无法完成。(OSStatus 错误 -12792.)", NSLocalizedFailureReason=发生未知错误 (-12792)}
  • @Sergey_Hot_UA 对我来说,同样的问题有时会起作用,有时会引发同样的错误。你找到解决办法了吗?
  • AVFoundation 的某些部分也可能不是线程安全的。您使用了哪些具体的类和方法?
【解决方案2】:

如果将 imageref 移到 async 部分内会更好吗?喜欢:

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);
        }
    });
});

【讨论】:

  • 是的显示,但是现在主线程很忙所有界面在加载图像之前都是不可点击的。
猜你喜欢
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 2014-11-20
  • 2018-06-14
  • 2012-12-06
  • 2017-02-16
  • 1970-01-01
相关资源
最近更新 更多