MPMoviePlayerControlle thumbnailImageAtTime: timeOption: giving empty UIImage[英] MPMoviePlayerControlle thumbnailImageAtTime: timeOption: giving empty UIImage

本文是小编为大家收集整理的关于MPMoviePlayerControlle thumbnailImageAtTime: timeOption: giving empty UIImage的处理方法,想解了MPMoviePlayerControlle thumbnailImageAtTime: timeOption: giving empty UIImage的问题怎么解决?MPMoviePlayerControlle thumbnailImageAtTime: timeOption: giving empty UIImage问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我正在使用它来获取视频的预览缩略图:

- (void)createThumb {
NSInteger paddingLeft = 22;
NSInteger paddingTop = 22;

CGFloat frameWidth = self.preview.frame.size.width - (2 * paddingLeft); 
CGFloat frameHeight = self.preview.frame.size.height - (2 * paddingTop);

CGSize size = CGSizeMake(frameWidth, frameHeight);


MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] 
                               initWithContentURL:self.fileUrl];

NSLog(@"File url %@",self.fileUrl);

UIImage *thumbnail = [mp thumbnailImageAtTime:0.0 
                                   timeOption:MPMovieTimeOptionNearestKeyFrame];
[mp stop];
[mp release];
if (!thumbnail){
    thumbnail = [UIImage imageNamed:@"video_dummy"];
}
  thumb = [[thumbnail gtm_imageByResizingToSize:size preserveAspectRatio:YES trimToFit:NO] retain];
}

但它不起作用.生成的 UIImage 始终为零.有人可以帮忙吗?

谢谢,菲利普

推荐答案

好的,我找到了解决方案:

我使用它而不是使用 MPMoviePlayerController,它的工作要好得多:

 NSInteger paddingLeft = 24;
NSInteger paddingTop = 24;

CGFloat frameWidth = self.preview.frame.size.width - (2 * paddingLeft); 
CGFloat frameHeight = self.preview.frame.size.height - (2 * paddingTop);

CGSize size = CGSizeMake(frameWidth, frameHeight);

AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.fileUrl options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    if (result == AVAssetImageGeneratorSucceeded) {
        UIImage *thumbnail=[[UIImage imageWithCGImage:im] retain];
        thumb = [[thumbnail gtm_imageByResizingToSize:size preserveAspectRatio:YES trimToFit:NO] retain];

        NSData *thumbData = UIImageJPEGRepresentation(thumb, 0.2);

        [thumbData writeToFile:  [[[NSFileManager defaultManager] contentDirectory] stringByAppendingPathComponent:self.thumbFilename] atomically: NO];
        [generator release];
        [self.preview setImage: thumb];   
    }
    else {
        UIImage *thumbnail=[[UIImage imageNamed:@"video_dummy"] retain];
        thumb = [[thumbnail gtm_imageByResizingToSize:size preserveAspectRatio:YES trimToFit:NO] retain];

        NSData *thumbData = UIImageJPEGRepresentation(thumb, 0.2);

        [thumbData writeToFile:  [[[NSFileManager defaultManager] contentDirectory] stringByAppendingPathComponent:self.thumbFilename] atomically: NO];
        [generator release];
        [self.preview setImage: thumb];
    }
};

CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];    

因此,如果您想这样做,请使用 AVFramework 方法

本文地址:https://www.itbaoku.cn/post/924410.html