English 中文(简体)
制作视频节目——加速表演——AVAsset-(复制)
原标题:Creating Thumbnail from Video - Improving Speed Performance - AVAsset - iPhone [duplicate]

我在以下读物中使用基于该守则的代码制作录像带:

从一个录像带或Pi SDK的数据中提取一个th。

我的法典如下:

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 ?

谢谢。

问题回答

这是一部通用的法典,你只是通过媒体档案的途径,确定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;
}

迅速解决:

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




相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

热门标签