这是我的代码,它完美地运作。在这里,我在我的tableview中访问一系列视频......
这是我的一系列视频和视频... ...
- (void)viewDidLoad {
[super viewDidLoad];
videos = [[NSMutableArray alloc] initWithObjects:
@"video 1",
@"video 2",
nil];
}
在tableview中访问视频.....
- (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访问其他视图中的视频......
- (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] ;
}
}
完成....
答案 0 :(得分:1)
如果“视频”是ALAsset的实例,那么你可以使用 - (CGImageRef)缩略图方法:
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具有type属性和thumb属性,以及URL到资产。
要使用ALFoundation,您应该导入此库:
@import AVFoundation;
要使用AssetsLibrary,您应该导入此库:
@import AssetsLibrary;
此外,我认为您应该在编写代码之前阅读提供的链接。
答案 1 :(得分:0)
显示您可以执行的视频缩略图,我添加了Swift和Objective-C代码以生成视频的缩略图。
Swift 3.X和4.0
do {
let asset = AVURLAsset(url: videoURL as! URL)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
let cgImage = try imageGenerator.copyCGImage(at: CMTimeMake(3, 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
imgView.image = thumbnail
}catch{
print("Error is : \(error)")
}
videoURL
是视频文件的路径。
Objective-C代码:
-(UIImage *)generateThumbImage : (NSString *)filepath
{
NSURL *url = [NSURL fileURLWithPath:filepath];
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CMTime time = [asset duration];
time.value = 1000; //Time in milliseconds
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); // CGImageRef won't be released by ARC
return thumbnail;
}