在UITableViewCell上异步生成视频封面可以通过使用AVAssetImageGenerator来实现。以下是一种实现方式:

  1. 首先,你需要在UITableViewCell的子类中添加一个UIImageView作为视频封面的显示视图。例如,你可以在UITableViewCell的初始化方法中添加以下代码:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 添加视频封面显示视图
        self.videoCoverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; // 自定义尺寸
        [self.contentView addSubview:self.videoCoverImageView];
    }
    return self;
}
  1. 在UITableViewCell的子类中,你需要为视频链接异步生成视频封面。你可以创建一个方法来处理这个任务,例如:
- (void)generateVideoCoverImageWithURL:(NSURL *)videoURL {
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    imageGenerator.appliesPreferredTrackTransform = YES;
    
    // 异步生成视频封面
    [imageGenerator generateCGImagesAsynchronouslyForTimes:@[[NSValue valueWithCMTime:kCMTimeZero]] completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {
        if (result == AVAssetImageGeneratorSucceeded) {
            // 更新视频封面显示视图
            dispatch_async(dispatch_get_main_queue(), ^{
                self.videoCoverImageView.image = [UIImage imageWithCGImage:image];
            });
        }
    }];
}
  1. 最后,在UITableViewDataSource的代理方法中,你可以调用上述方法来为每个UITableViewCell的视频链接生成视频封面。例如:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YourCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YourCustomTableViewCellIdentifier" forIndexPath:indexPath];
    
    // 获取当前行的视频链接
    NSURL *videoURL = [self.videoURLs objectAtIndex:indexPath.row];
    
    // 异步生成视频封面
    [cell generateVideoCoverImageWithURL:videoURL];
    
    return cell;
}

这样,每个UITableViewCell都会在异步生成视频封面后更新相应的UIImageView,而不会影响复用和退出当前页面。

uitableviewcell 上video视频链接mp4异步生成视频封面 不影响复用和退出当前页面 object-c

原文地址: https://www.cveoy.top/t/topic/i9RT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录