【问题标题】:video thumbnail image not generated from local video URL(asset URL) iOS视频缩略图不是从本地视频 URL(资产 URL)生成的 iOS
【发布时间】:2016-04-06 11:08:20
【问题描述】:

我正在使用 AVAssetImageGenerator 来生成视频的缩略图。当我通过本地视频资产 url 生成图像时,它正在重新调整 nil。我正在使用以下代码生成图像。

 NSURL *url = [NSURL fileURLWithPath:filepath];

AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time =CMTimeMake(1, 1);

CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

【问题讨论】:

    标签: ios xcode avassetimagegenerator


    【解决方案1】:

    只需调用此方法即可创建视频缩略图。

    //Utilities.m

     +(void)thumbnailImageForVideoURL:(NSString *)videoURL completionHandler:(void (^)(UIImage *))completionHandler{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    
        NSURL *url = [NSURL URLWithString:videoURL];
        AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
        AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
        generator.appliesPreferredTrackTransform=TRUE;
    
        CMTime thumbTime = CMTimeMakeWithSeconds(30,30);
    
        AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    
            if (result != AVAssetImageGeneratorSucceeded) {
                NSLog(@"couldn't generate thumbnail, error:%@", error);
            }
    
            UIImage *image;
            if(im){
                image = [UIImage imageWithCGImage:im];
    
            }else{
                image = [UIImage imageNamed:@"videoPlaceholder"];
            }
            completionHandler(image);
        };
    
        CGSize maxSize = CGSizeMake(568, 426);
        generator.maximumSize = maxSize;
        [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
    });
    
    }
    

    你班上的电话。

    #import "Utilities.h"
    
    [Utilities thumbnailImageForVideoURL:moviePath completionHandler:^(UIImage *thumbImage) {
    
                            dispatch_async(dispatch_get_main_queue(), ^{
    
                                self.imgViewContent.image = thumbImage;
                                //self.objectDetail.videoThumbImage = UIImageJPEGRepresentation(thumbImage, 1.0);
                            });
    
                        }];
    

    【讨论】:

    【解决方案2】:

    使用下面的代码

    AVAsset *asset = [AVAsset assetWithURL:url];
        //  Get thumbnail at the very start of the video
        CMTime thumbnailTime = [asset duration];
        thumbnailTime.value = 0;
    
        //  Get image from the video at the given time
        AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
        CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL];
    
        UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
    

    【讨论】:

    • CMTime 是总持续时间?
    • 欢迎来到 SO。您能否解释一下为什么以及如何解决 OPs 问题?
    • @Va 谢谢。它奏效了。但是请您解释一下 CMTime 是如何工作的。
    • @Va Team 在我的情况下我得到“[BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C2.1:2][0x7fede2417020] 输出帧失败,状态 8196”
    【解决方案3】:

    试试这个代码

    -(UIImage *)generateThumbImage : (NSString *)filepath{
        NSURL *url = [NSURL fileURLWithPath:filepath];
    
        UIImage *theImage = nil;
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
        AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
        generator.appliesPreferredTrackTransform = YES;
        NSError *err = NULL;
        CMTime time = CMTimeMake(1, 60);
        CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];
    
        theImage = [[UIImage alloc] initWithCGImage:imgRef] ;
    
        CGImageRelease(imgRef);
        return theImage;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-01
      • 2019-05-06
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 2016-09-17
      • 2012-09-01
      相关资源
      最近更新 更多