【问题标题】:Adding multiple videos to uicollectionview - objective-c将多个视频添加到 uicollectionview -objective-c
【发布时间】:2016-11-08 18:11:28
【问题描述】:

我在将多个视频添加到 uicollectionview 时遇到问题。 我可以将多个图像添加到 uicollectionview,并且它们可以正确显示。 但是,当我尝试添加多个视频时,只会播放添加的最后一个视频。

// 从手机图库中选择图片/视频

- (void)chooseExistingImagesButton:(id)sender {

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, (NSString *)kUTTypeVideo, (NSString*)kUTTypeImage, nil];
    picker.allowsEditing = NO;
    picker.delegate = self;

    [self presentViewController:picker animated:YES completion:nil];
}
else {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"Sorry, we cannot detect a camera on this device." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
}

[imageCollectionView reloadData];
[videoCollectionView reloadData]; }

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self targetImageArray];
[self targetVideoArray];

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    NSString *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [targetImageArray addObject:selectedImage];
    [targetImageArray componentsJoinedByString:@","];
}
else if (([mediaType isEqualToString:(NSString *)kUTTypeVideo]) || ([mediaType isEqualToString:(NSString *)kUTTypeMovie])) {
    NSString *moviePath = [info objectForKey:UIImagePickerControllerMediaURL];
    [targetVideoArray addObject:moviePath];
    [targetVideoArray componentsJoinedByString:@","];
}

[imageCollectionView reloadData];
[videoCollectionView reloadData];

[self dismissViewControllerAnimated:YES completion:^{NSLog(@"Finished Picking Image or Video");}]; }

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:^{NSLog(@"Image picker view dismissed");}]; }

// 加载集合视图

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1; }

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (collectionView.tag == ImageCell) {
    return [targetImageArray count];
}
if (collectionView.tag == VideoCell) {
    return [targetVideoArray count];
}
return 0; }

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

if (collectionView.tag == VideoCell) {
    static NSString *videoCellIdentifier = @"VideoCell";
    TargetVideoCell *videoCell = [collectionView dequeueReusableCellWithReuseIdentifier:videoCellIdentifier forIndexPath:indexPath];

    movieUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@", [targetVideoArray objectAtIndex:indexPath.row]]];

    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:movieUrl options:nil];
    AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
    generator.appliesPreferredTrackTransform = YES;
    UIImage *image = [UIImage imageWithCGImage:[generator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil]];

    videoCell.targetMovieView.image = image;

    [moviePlayer.view setFrame:videoCell.targetMovieView.frame];
    moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];

    return videoCell;
}


if (collectionView.tag == ImageCell) {
    static NSString *imageCellIdentifier = @"ImageCell";
    TargetImageCell *imageCell = [collectionView dequeueReusableCellWithReuseIdentifier:imageCellIdentifier forIndexPath:indexPath];

    imageCell.targetImageView.image = [targetImageArray objectAtIndex:indexPath.row];
    return imageCell;
}

return 0; }

// 播放视频

- (IBAction)playMovie:(id)sender {
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; }

- (void)playMovieFinished:(NSNotification *)theNotification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;
[self dismissMoviePlayerViewControllerAnimated]; }

【问题讨论】:

  • moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl]; 这似乎是罪魁祸首。只有“最后一个”有moviePlayer。而当您放置图像时,单元格有自己的属性来显示图像。另外,为什么您的问题被标记为iPadCoreData
  • 感谢您的回复,我正在将图像和视频数组保存到 CoreData,用于 iPad 应用程序。第一次在这里发帖,想如果你添加更多标签,它会接触到更多的人:)
  • 问题是您的问题不适用于 iPad(它也可能在 iPhone 上),并且与 CoreData 无关(因为您甚至没有放置一行 CoreData)。它们是无关的。

标签: ios objective-c video


【解决方案1】:

在每个 cellForRow 上,您再次初始化您的电影播放器​​。 moviePlayer - 这是你班级的财产。

解决方案:

  1. 每个单元都必须有自己的电影播放器​​。

    videoCell.movi​​ePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl]; [videoCell.movi​​ePlayer.view setFrame:videoCell.targetMovieView.bounds];

  2. 有一个电影播放器​​和捕捉按钮点击事件

【讨论】:

  • 感谢您的回复,我已经更新了上面的代码,但我现在无法播放视频。 (不是因为缺乏尝试)我收到以下错误: - 没有可见的@interface 声明选择器dismissMoviePlayerViewControllerAnimated - 尝试在 上呈现 其视图不在窗口层次结构中!我也能够让播放器加载,但它加载一个空白屏幕。
  • 感谢您的帮助,让它工作:- (void)playVideo:(id)sender { [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer ]; moviePlayer.controlStyle = MPMovieControlStyleDefault; moviePlayer.shouldAutoplay = YES; [self addSubview:moviePlayer.view]; [moviePlayer setFullscreen:YES 动画:YES]; }
  • - (void)moviePlayBackDidFinish:(NSNotification*)notification { MPMoviePlayerController *player = [通知对象]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player]; if ([player respondsToSelector:@selector(setFullscreen:animated:)]) { [player.view removeFromSuperview]; } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2020-09-27
相关资源
最近更新 更多