0

写真とビデオにはImagePickerControllerがあります。私がカメラから写真やビデオを選ぶとき、画像としてCollectionViewに表示します。このステップではすべてが動作します。 変数checkVideoを追加して、私がセル内の画像、ビデオまたは写真の画像を知ることができます。 checkVideoビデオや写真にimagePickerControllerを使用すると、値が変更されます。私はビデオから来る画像の再生アイコンを使って新しい画像を追加したい。ここに私のコードです:CollectionViewCellの2つのイメージビューを設定する方法

- (CustomCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; 

    if (checkVideo == YES) { 

     UIImageView *playVideoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     playVideoImageView.image = playImg; 
     [cell addSubview:playVideoImageView]; 
     cell.imageViewCell.image = arrayForImages[indexPath.row]; 
     checkVideo = NO; 
    } else { 
     cell.imageViewCell.image = arrayForImages[indexPath.row]; 
    } 
    NSLog(@"check video %@", checkVideo ? @"YES" : @"NO"); 
    return cell; 
} 
+0

ここで、この 'checkVideo'フラグを設定していますか?それは単一の変数か '配列'ですか? – Mathews

答えて

1

次の2つのプロトタイプ、代わりのものを作る必要があるかもしれません

- (CustomCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell1" forIndexPath:indexPath]; 

    // previously, you were using the same variable, that always had the same value for each row... 
    // you should grab a value from your prepared array like this: 

    Bool checkVideo = checkVideoArray[indexPath.row] 

    if (checkVideo == YES) { 
     cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell2" forIndexPath:indexPath];   
     cell.imageViewCell.image = arrayForImages[indexPath.row]; 

     // in your prototype MyCell2 you now have to add one more subView like 'playImageView' 
     // don't forget to link it via @IBOutlet too 
     // reuse handling will be safe here - because if you were adding subview each time in cellForRow method, the image wouldn't be handled properly and it would be re-adding a layer on layer, what wouldn't be OK 
     cell.playImageView.image = playImg;       
    } else { 
     cell.imageViewCell.image = arrayForImages[indexPath.row]; 
    } 
    NSLog(@"check video %@", checkVideo ? @"YES" : @"NO"); 
    return cell; 
} 
+0

私はあなたのソリューションを使用しようとしましたが、私は写真をすべてうまく取るときに同じ結果を持っていますが、ビデオを使用するとすべてのセルに再生アイコンが追加されます。しかし、もう一つ質問があります。私はxibを使用する場合、私は再生画像(アイコン)のための別のカスタムセルを追加する必要がありますまたは私は1つのカスタムセルを使用することができますとちょうどnib '[self.collectionView registerNib:[UINib nibWithNibName:@ "CustomCell" bundle:nil] forCellWithReuseIdentifier: "MyCell2"]; ' –

+0

私は気づいていない、私たちはあなたのcheckVideo変数のための正しい値を得ることを忘れていた。アップデートをチェックする。 – pedrouan

+0

ありがとうございます!それは完璧に動作します!あなたは私の日を救う! –

0

あなたは現在直面している問題は何ですか?

カスタムセルに1つのイメージビューを追加し、checkvideo boolに基づいてイメージを非表示にすることができます。

- (CustomCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; 

    if (checkVideo == YES) { 

     /* UIImageView *playVideoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     playVideoImageView.image = playImg; 
     [cell addSubview:playVideoImageView];*/ 
     cell.imageViewCell.image = arrayForImages[indexPath.row]; 
     checkVideo = NO; 
    } else { 
     cell.imageViewCell.image = arrayForImages[indexPath.row]; 
    } 
    NSLog(@"check video %@", checkVideo ? @"YES" : @"NO"); 

//以下の行に基づいて再生画像を非表示にすることができます。 cell.playimage.hidden =!checkVideo;そして、ストーリーボードのcollectionViewシーンで MyCell1とMyCell2:

return cell; 
} 
+0

私は写真を撮る場合、最初に再生ボタンが正しく表示されないが、再生ボタンがすべてのセルに表示されるビデオを録画すると、この方法を試してみる。 –

+0

私のコードを 'if(checkVideo == YES){ セルに変更する。 playImage.hidden =!checkVideo; cell.imageViewCell.image = arrayForImages [indexPath.row]; } else { cell.imageViewCell.image = arrayForImages [indexPath.row]; } checkVideo = NO; 戻りセル; 'ビデオを録画するときに最初のセルに追加されたアイコンを再生し、1、2、3セルなどに移動した写真再生アイコンを追加した後 –

+0

as Videoを表示するには、 複数の行がある場合は、問題が発生する可能性があります。arrayForVideoを管理する必要があります。これは、arrayforimageと同じ数のboolオブジェクトを持ちます。 if([arrayForVideo objectatindex item] == YES) – Bhavik

1

私はそれを見る方法を、あなたはシングルを使用したかもしれませんBoolean変数checkVideoは、ビデオと写真を区別します。それが写真でうまく動作する理由かもしれませんが、ビデオが記録されると、すべての画像が再生ボタンでビデオとしてキャストされます。おそらく、フラグをブール配列に再設計する必要があるかもしれません。 フラグを次のように設定します。

[checkVideo replaceObjectAtIndex:index withObject:[NSNumber numberWithBool:YES]] 

またはのNSNumber

[checkVideo replaceObjectAtIndex:index withObject:@(YES)]] 

にBOOLをラップ@(YES)を用いて今からBOOLVALUEを引き抜くことにより、考慮中のセルに対応するcheckVideoフラグインスタンスの値をチェックすることができます配列。

BOOLビデオ= [[checkVideo objectAtIndex:indexPath.row] boolValue];

if video == YES{ 
    //do your stuff here 
} 

PS:これらの事実をすべて質問に記載するか、コメント内の説明に回答してください。これはピンが正確なエラーを指すのに役立ちます。

+0

私はmutable配列を初期化してbool値を追加しますが、書くときに比較しようとすると警告が出ます**ポインタと整数( 'id'と 'int')の比較** my code 'if(checkVideoArray [ indexPath.row] == YES) ' –

+0

も回答に追加されました – Mathews

+0

ありがとうございました!私は@pedrouanの解決策から私の問題を解決する。しかし、あなたの解決策もそうです!ありがとうございました! –

関連する問題