2016-11-07 11 views
0

複数の動画をuicollectionviewに追加する際に問題が発生しています。 複数の画像をuicollectionviewに追加すると、正しく表示されます。 複数の動画を追加しようとすると、最後に追加された動画のみが再生されます。すべてのcellForRowの電話のギャラリーから複数の動画をuicollectionviewに追加する - objective-c

//選択した画像/動画

- (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");}]; } 

//ロードcollectionview

- (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]; } 
+0

'のMoviePlayer = [movieUrl [MPMoviePlayerViewControllerのalloc] initWithContentURL]をタップボタンのキャッチイベントがあります。最後のものだけが 'moviePlayer'を持っています。イメージを置くと、セルにはイメージを表示するための独自のプロパティがあります。また、なぜあなたの質問は 'iPad'と' CoreData'でタグ付けされていますか? – Larme

+0

あなたの応答をありがとう、私は両方の画像とビデオアレイをCoreDataにiPadアプリのために保存しています。初めてここに投稿すると、タグを追加すると、それ以上の人々に手を差し伸べるだろうと思った:) – Stephen

+0

問題はあなたの問題がiPadにも適切ではないということです。 (CoreDataの行を配置していないため)。彼らは無関係です。 – Larme

答えて

0

ムービーを初期化再びプレイヤー。 moviePlayer - これはあなたのクラスのプロパティです。

解決策:

  1. すべての細胞必見ムービープレーヤーを所有しています。

    videoCell.movi​​ePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl]; [videoCell.movi​​ePlayer.view setFrame:videoCell.targetMovieView.bounds]; `犯人であると考えられる;:

  2. は、1人のムービープレーヤーと

+0

返信いただきありがとうございます。私は上記のコードを更新しましたが、今再生するビデオは取得できません。 (ないようにしようの欠如のために)私は、次のようなエラーになっていないよ: を - 目に見える@interfaceをセレクタが dismissMoviePlayerViewControllerAnimatedを宣言するために - 提示する試みを、そのビューではありませんウィンドウ階層! また、プレーヤーに読み込ませることもできましたが、空の画面が読み込まれます。 – Stephen

+0

あなたの助けのおかげで、それが働いてしまった: - (無効)playVideo:(ID)、送信者{ [[NSNotificationCenter defaultCenter] addObserver:自己 セレクター:@selector(moviePlayBackDidFinish :) 名:MPMoviePlayerPlaybackDidFinishNotification オブジェクト:のMoviePlayer] ; moviePlayer.controlStyle = MPMovieControlStyleDefault; moviePlayer.shouldAutoplay = YES; [self addSubview:moviePlayer。ビュー]; [moviePlayer setFullscreen:YES animated:YES]; } – Stephen

+0

- (void)moviePlayBackDidFinish:(NSNotification *)通知{ MPMoviePlayerController * player = [通知オブジェクト]; [[NSNotificationCenter defaultCenter] removeObserver:自称:MPMoviePlayerPlaybackDidFinishNotificationオブジェクト:player]; if([player respondsToSelector:@selector(setFullscreen:animated :)]){ [player.view removeFromSuperview]; }} – Stephen

関連する問題