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).
Funziona, grazie. – c0d3Junk13
@ c0d3Junk13 siete i benvenuti :) – sankalp