如何检查UICollectionView是否有图像

时间:2019-03-10 04:52:09

标签: ios objective-c delegates uicollectionviewcell

我有4个UICollectionViewCell,在其中我想使前2个单元格具有强制性的图像。示例代码如下。下面是一个用例:

有4个UICollectionViewCell,如果我单击每个单元格将打开相机,我们可以拍照或从图库中选择图像。如何知道前2个单元格必须有图像。寻找条件以检查此逻辑。

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

    Image *image = (Image *)self.defect.imageSet[indexPath.item];
    ADRPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ADRPhotoCollectionViewCell class]) forIndexPath:indexPath];
    cell.image = image.thumbnailImage ? image.thumbnailImage : nil;
    cell.photoType = [self.defect defectPhotoTypeForIndex:indexPath];
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

   Image *image = self.defect.imageSet[indexPath.item];

    if (self.selectedImage != image) {

       [self updateAnnotatedImage];
        self.selectedImage = image;
       [self.jotController clearAll];
       [self updateUserInterface];

    if (image.thumbnailImage) {
        [self transitionImage:YES];
    } else {
        self.imageView.image = nil;
        [self showCameraPicker];
    }

} else if (!image.thumbnailImage) {
      //clicked the + to add a new image
      [self showCameraPicker];
  }
}

1 个答案:

答案 0 :(得分:0)

您可以通过这种方式检查UIImageView中是否有图像:

-(BOOL) checkImages {
    for (int i=0, i<2 , i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        ADRPhotoCollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        if (cell.imageView.image == nil || CGSizeEqualToSize(cell.imageView.image.size, CGSizeZero)) {
            //there isn't any image or the image is empty
            return NO;
        }
    }
    return YES;
}