繁体   English   中英

从线程中的视频网址中提取缩略图

[英]Extract thumbnail from video url in threads

我在另一个线程中使用(Grand Central Dispatch)在我的MPMoviePlayer上遇到问题。 我想通过网址吸引视频,剪切第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)show 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)}

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

如果在异步部分中移动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);
        }
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM