gpt4 book ai didi

ios - 在tableviewcell中显示视频的缩略图

转载 作者:行者123 更新时间:2023-11-29 02:18:11 25 4
gpt4 key购买 nike

这是我的代码,运行完美。在这里,我正在访问我的 tableview 中的视频数组......

这是我的一系列视频,视频在我的 bundle 中...

        - (void)viewDidLoad {
[super viewDidLoad];
videos = [[NSMutableArray alloc] initWithObjects:
@"video 1",
@"video 2",

nil];
}

在桌面 View 中访问视频......

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSIndexPath *index ;

index = [self.table indexPathForCell:cell];
NSLog(@"%@----hello",indexPath);

// taking path of video from bundle
NSString *filePath = [[NSBundle mainBundle]pathForResource:[videos objectAtIndex:indexPath.row] ofType:@"mp4"];

//declare nsurl and mpmoviplayer globally..
movieURL = [NSURL fileURLWithPath:filePath];


// <converting image in thumbnail...
AVAsset* videoAsset = [AVAsset assetWithURL:movieURL];

AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:videoAsset];

Float64 durationSeconds = CMTimeGetSeconds([videoAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
NSError* error = nil;
CMTime actualTime;

//generating image...
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];


// show image in uiimage of tableviewcell....
cell.imageView.image=[UIImage imageWithCGImage:halfWayImage];
cell.textLabel.text= [videos objectAtIndex:indexPath.row];


return cell;

}

通过Segue访问其他 View 中的视频......

        - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDetailsSeg"]) {

//playing video in other view controller....

[[player view] setFrame: self.view.frame];
[self.view addSubview: [player view]];

[self presentMoviePlayerViewControllerAnimated:player];

//using sequel for destination controller
[segue destinationViewController] ;
}
}

完成....

最佳答案

如果“视频”是 ALAsset 的实例,那么您可以使用 - (CGImageRef)thumbnail 方法:

UIImage* thumb = [UIImage imageWithCGImage:[video thumbnail]];

如果你使用AVAsset来呈现视频对象,你可以使用AVAssetImageGenerator在特定时间获取图像并使用此图像生成 thumb .

你可以这样做:

dispatch_async(dispatch_get_main_queue(), ^{
NSURL* urlToVideo = [NSURL URLWithString:@"file://localPathToVideo"];
AVAsset* videoAsset = [AVAsset assetWithURL:urlToVideo];

AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:videoAsset];

Float64 durationSeconds = CMTimeGetSeconds([videoAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
NSError* error = nil;
CMTime actualTime;

CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];

UIImage* resultUIImage = nil;
if (halfWayImage != NULL) {
resultUIImage = [UIImage imageWithCGImage:halfWayImage];

CGImageRelease(halfWayImage);

//resize image (use some code for resize)
//<>
//TODO: call some method to resize image
UIImage* resizedImage = resultUIImage;
//<>

dispatch_async(dispatch_get_main_queue(), ^{
//TODO: set resized image to destination
//if(cell.asset == videoAsset)
//{
// cell.thumb.image = resizedImage
//}
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
//TODO: do something if you can't generate preview.
});
}
});

UPD


解释,为什么开发人员应该使用 ALAsset 或 AVAsset 在应用程序中呈现视频。视频是结构复杂的二进制文件,要访问视频数据,您可以编写自己的库,并使用您的方法从视频中生成缩略图。但是 iOS SDK 提供了一些处理视频的方法。该 SDK 包含 ALFoundation 框架,它允许在任何路径(在您认为在 Documents 目录中)处理视频,您只需要知道视频 URL(或本地路径)。如果你从 AssetsLibrary 获取视频(照片),那么你就有了 ALAsset(展示了来自图书馆的一些项目),ALAsset 有类型属性和缩略图属性,以及 Assets 的 URL。


要使用 ALFoundation,你应该导入这个库:

@import AVFoundation;

要使用 AssetsLibrary,您应该导入此库:

@import AssetsLibrary;

此外,我认为您应该在编写代码之前阅读提供的链接。

关于ios - 在tableviewcell中显示视频的缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28493079/

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