2013-10-14 14 views
5

Sto usando questo per riferimento: Getting thumbnail from a video url or data in IPhone SDKGenerazione miniatura dal video - iOS7

Il metodo è utilizzando la classe MPMoviePlayerController al posto del AVFoundation, ed io credo di voler utilizzare tale anche perché la gente ha detto che MPMoviePlayer modo è più veloce del modo AVFoundation.

Il problema è che il metodo utilizzato per creare le anteprime, [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame] è obsoleto in iOS 7.0.

Osservando i documenti Apple, i restanti modi supportati per creare miniature sono con i metodi (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option e (void)cancelAllThumbnailImageRequests. Ma, come dettano le firme del metodo, questi metodi non restituiscono nulla. Quindi, come posso accedere alla miniatura UIImage creata con questi metodi?

Se aiuta, questo è quello che ho finora in termini di codice:

self.videoURL = info[UIImagePickerControllerMediaURL]; 
    NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL]; 

    //Create thumbnail image 
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL]; 
    [player requestThumbnailImagesAtTimes:@[@1] timeOption:MPMovieTimeOptionNearestKeyFrame]; 
    //UIImage *thumbnail = ??? 

Come faccio ad avere un riferimento UIImage alla miniatura?

EDIT ho capito come creare una notifica per la richiesta di immagine in miniatura (usando this question come riferimento). Tuttavia, mi rendo conto che questo metodo funziona in modo asincrono dal thread principale, e quindi il mio metodo di gestione delle notifiche non sembra essere mai chiamato.

Questo è quello che ho ora.

self.videoURL = info[UIImagePickerControllerMediaURL]; 
    NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL]; 
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleThumbnailImageRequestFinishNotification:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:player]; 
    [player requestThumbnailImagesAtTimes:@[@1] timeOption:MPMovieTimeOptionNearestKeyFrame]; 

E poi il mio metodo del gestore:

-(void)handleThumbnailImageRequestFinishNotification:(NSNotification*)notification 
{ 
    NSDictionary *userinfo = [notification userInfo]; 
    NSError* value = [userinfo objectForKey:MPMoviePlayerThumbnailErrorKey]; 
if (value != nil) 
{ 
    NSLog(@"Error creating video thumbnail image. Details: %@", [value debugDescription]); 
} 
else 
{ 
    UIImage *thumbnail = [userinfo valueForKey:MPMoviePlayerThumbnailImageKey]; 
} 

Ma il gestore non viene mai chiamato (o almeno così sembra).

risposta

1

Se il tuo URL è in streaming HTTP, non restituirà nulla, secondo i documenti. Per un URL del file, ho scoperto che dovevo avviare la richiesta dopo averlo visto, altrimenti non sarebbe mai stato chiamato.

14

Provare in questo modo.

import AVFoundation framework 

in * .h

#import <AVFoundation/AVFoundation.h> 

in * .m

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:self.urlForConevW options:nil]; 
AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
NSError *error = NULL; 
CMTime time = CMTimeMake(1, 65); 
CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error]; 
NSLog(@"error==%@, Refimage==%@", error, refImg); 

UIImage *FrameImage= [[UIImage alloc] initWithCGImage:refImg]; 
+1

Funziona, grazie. – c0d3Junk13

+0

@ c0d3Junk13 siete i benvenuti :) – sankalp

3

Ecco un codice per rendere una miniatura del video e salvare le immagini al DocumentDirectory ..

//pass the video_path to NSURL 
NSURL *videoURL = [NSURL fileURLWithPath:strVideoPath]; 

AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset1]; 
generator.appliesPreferredTrackTransform = YES; 

//Set the time and size of thumbnail for image 
NSError *err = NULL; 
CMTime thumbTime = CMTimeMakeWithSeconds(0,30); 
CGSize maxSize = CGSizeMake(425,355); 
generator.maximumSize = maxSize; 

CGImageRef imgRef = [generator copyCGImageAtTime:thumbTime actualTime:NULL error:&err]; 
UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imgRef]; 

//And you can save the image to the DocumentDirectory 
NSData *data = UIImagePNGRepresentation(thumbnail); 

//Path for the documentDirectory 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
[data writeToFile:[documentsDirectory stringByAppendingPathComponent:currentFileName] atomically:YES]; 
Problemi correlati