Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Thumbnail from Video - Improving Speed Performance - AVAsset - iPhone [duplicate]

I am using code based on the code in the following thread to generate a video thumbnail :

Getting a thumbnail from a video url or data in iPhone SDK

My code is as follows :

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

}

The issue is that there is a delay of about 5-10 seconds in generating the thumbnail image. Is there anyway that I could improve the speed of this code and generate the thumbnail quicker ?

Thank you.

like image 943
GuybrushThreepwood Avatar asked Apr 19 '12 09:04

GuybrushThreepwood


1 Answers

This is a generic code, you should just pass a path for the media file and set the ratio between 0 and 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;
}
like image 109
pascalbros Avatar answered Sep 19 '22 13:09

pascalbros