2012-04-19 12 views
7

używam kodu na podstawie kodu w wątku generować miniatury wideo:generowaniu miniatury z wideo - Poprawa prędkości Performance - AVAsset - iPhone

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

Mój kod wygląda następująco:

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

} 

Problem polega na tym, że generowanie miniatury trwa około 5-10 sekund. Czy mimo to mogę poprawić szybkość tego kodu i szybciej wygenerować miniaturę?

Dziękuję.

Odpowiedz

16

Poniższy kod przedstawia tworzenie kciuków filmów wideo.

NSString *str = [[self.vedioArray objectAtIndex:i] valueForKey:@"vName"]; 
    NSURL *videoURL = [NSURL URLWithString:str] ; 
    MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] initWithContentURL:videoURL]autorelease]; 
    UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; 
    player = nil; 

Also answer given here

+0

Znacznie szybciej - dziękuję. – GuybrushThreepwood

+0

+1 pomógł mi również – Charan

+1

Nie otrzymuję miniatury tego adresu URL: http://www.sciencentral.com/news/image_db/2024515/NNM2212_AntPower_MSTR.mov ..Czy możesz sprawdzić? – parilogic

11

Jest to ogólny kod, należy po prostu przekazać ścieżkę do pliku multimedialnego i ustawienie proporcji pomiędzy 0 a 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; 
} 
3

Swift rozwiązanie:

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 
    } 
} 
Powiązane problemy