Как проверить, что UICollectionView имеет изображение - PullRequest
0 голосов
/ 10 марта 2019

У меня есть 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 Ответ

1 голос
/ 10 марта 2019

Вы можете проверить, есть ли изображение в 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;
}
...