1

我正在使用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);
4

3 回答 3

3

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

//实用程序.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);
                        });

                    }];
于 2016-01-02T08:43:10.807 回答
0

使用下面的代码

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);
于 2016-03-09T12:36:24.420 回答
0

试试这个代码

-(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;
}
于 2016-01-02T08:38:15.790 回答