7

我正在使用基于以下线程中的代码的代码来生成视频缩略图:

从 iPhone SDK 中的视频 url 或数据获取缩略图

我的代码如下:

if (selectionType == kUserSelectedMedia) {

    NSURL * assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];

    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:assetURL options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform=TRUE;
    [asset release];
    CMTime thumbTime = CMTimeMakeWithSeconds(0,30);

    //NSLog(@"Starting Async Queue");

    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
        if (result != AVAssetImageGeneratorSucceeded) {
            NSLog(@"couldn't generate thumbnail, error:%@", error);
        }

        //NSLog(@"Updating UI");

        selectMediaButton.hidden = YES;
        selectedMedia.hidden = NO;
        cancelMediaChoiceButton.hidden = NO;
        whiteBackgroundMedia.hidden = NO;

        //Convert CGImage thumbnail to UIImage.
        UIImage * thumbnail = [UIImage imageWithCGImage:im];

        int checkSizeW = thumbnail.size.width;
        int checkSizeH = thumbnail.size.height;
        NSLog(@"Image width is %d", checkSizeW);
        NSLog(@"Image height is %d", checkSizeH);

        if (checkSizeW >=checkSizeH) {
            //This is a landscape image or video.
            NSLog(@"This is a landscape image - will resize automatically");
        }

        if (checkSizeH >=checkSizeW) {
            //This is a portrait image or video.
            selectedIntroHeight = thumbnail.size.height;
        }

        //Set the image once resized.
        selectedMedia.image = thumbnail;

        //Set out confirm button BOOL to YES and check if we need to display confirm button.
        mediaReady = YES;
        [self checkIfConfirmButtonShouldBeDisplayed];

        //[button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
        //thumbImg=[[UIImage imageWithCGImage:im] retain];
        [generator release];
    };

    CGSize maxSize = CGSizeMake(320, 180);
    generator.maximumSize = maxSize;
    [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
}

}

问题是生成缩略图图像有大约 5-10 秒的延迟。无论如何,我可以提高此代码的速度并更快地生成缩略图吗?

谢谢你。

4

2 回答 2

11

这是一个通用代码,您应该只传递媒体文件的路径并将比率设置在 0 和 1.0 之间。

+ (UIImage*)previewFromFileAtPath:(NSString*)path ratio:(CGFloat)ratio
{
    AVAsset *asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    CMTime duration = asset.duration;
    CGFloat durationInSeconds = duration.value / duration.timescale;
    CMTime time = CMTimeMakeWithSeconds(durationInSeconds * ratio, (int)duration.value);
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
    UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return thumbnail;
}
于 2014-12-06T18:09:31.610 回答
3

快速解决方案:

func previewImageForLocalVideo(url:NSURL) -> UIImage?
{
    let asset = AVAsset(URL: url)
    let imageGenerator = AVAssetImageGenerator(asset: asset)
    imageGenerator.appliesPreferredTrackTransform = true

    var time = asset.duration
    //If possible - take not the first frame (it could be completely black or white on camara's videos)
    time.value = min(time.value, 2)

    do {
        let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: nil)
        return UIImage(CGImage: imageRef)
    }
    catch let error as NSError
    {
        print("Image generation failed with error \(error)")
        return nil
    }
}
于 2015-08-08T21:28:25.320 回答