我有ImagePickerController
照片和视频。当我从相机中选择照片或视频时,我将其显示在CollectionView
图像中。在这一步一切正常。我添加Bool
变量checkVideo
以了解我在单元格中的图像,来自视频或照片的图像。当我将imagePickerController用于视频或照片时,checkVideo
会更改值。我想添加一个带有播放图标的新图像,用于来自视频的图像。这是我的代码:
- (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 :(得分:1)
您可能需要制作两个原型,而不是一个: 故事板的collectionView场景中的MyCell1和MyCell2,然后:
- (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;
}
答案 1 :(得分:1)
我看到它的方式,您可能会使用一个checkVideo
变量[checkVideo replaceObjectAtIndex:index withObject:[NSNumber numberWithBool:YES]]
来区分视频和照片。这可能是它适用于照片的原因,但是当录制视频时,所有图像都会通过“播放”按钮投放为视频。因此,您可能需要将标志重新设计为布尔数组。
如图所示设置标志。
[checkVideo replaceObjectAtIndex:index withObject:@(YES)]]
或使用@(YES)将BOOL包装在NSNumber中
checkVideo
现在,您可以通过从数组中拉出boolValue来检查与所考虑的单元格对应的if video == YES{
//do your stuff here
}
标志实例的值。
BOOL video = [[checkVideo objectAtIndex:indexPath.row] boolValue];
IndexSizeError: Index or size is negative or greater than the allowed amount
PS:请在问题中陈述所有这些事实或回答评论中的澄清。这将有助于确定准确的错误。
答案 2 :(得分:0)
您目前面临的问题是什么?
您可以在自定义单元格中添加一个图像视图,并根据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;
return cell;
}