video thumbnail image not generated from local video URL(asset URL) iOS

丶灬走出姿态 提交于 2019-12-24 15:33:46

问题


i am using AVAssetImageGenerator for generating the thumbnail images for video. when i pass local video asset url to generate image it is retuning nil.i am using below code to generate image.

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

回答1:


use below code

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



回答2:


Simply call this method to create Video Thumbnail.

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

}

Call from your class.

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

                    }];



回答3:


Try this code

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


来源:https://stackoverflow.com/questions/34564018/video-thumbnail-image-not-generated-from-local-video-urlasset-url-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!