gpt4 book ai didi

ios - MPMoviePlayerController 缩略图图像 AtTime : timeOption: giving empty UIImage

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:10 25 4
gpt4 key购买 nike

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

- (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 方法

关于ios - MPMoviePlayerController 缩略图图像 AtTime : timeOption: giving empty UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7178631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com