UICollectionView not showing images from CameraImage - Objective C

233 Views Asked by At

Searched the forum and could not find a suitable answer that works. I suspect something is wrong with the code. I am taking pictures with a camera and trying to show them in a UICollectionView. The collectionView has a cell with a UIImageView inside of it. Even when setting a static image and background colour I still don't see anything (not even the colour). Code is as follows:

 #import "CameraPageViewController.h"

 @interface CameraPageViewController ()

@end

@implementation CameraPageViewController

- (void)viewDidLoad {
   [super viewDidLoad];
// Do any additional setup after loading the view.
_imageList = [[NSMutableArray alloc] init];
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"PictureCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}


- (IBAction)takePhoto:(id)sender {

UIImagePickerController *imagePickController=[[UIImagePickerController alloc]init];
imagePickController.sourceType=UIImagePickerControllerSourceTypeCamera;
imagePickController.delegate=self;
imagePickController.allowsEditing=TRUE;
[self presentViewController:imagePickController animated:YES completion:nil];

}

- (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{

  UIImage *chosenImage = editingInfo[UIImagePickerControllerOriginalImage];

[_imageList addObject:chosenImage];

[self dismissViewControllerAnimated:YES completion:nil];


[_collectionView reloadData];


}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.imageList count];
}

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"PictureCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *pictureView = (UIImageView *) [cell viewWithTag:110];
NSInteger rowValue = indexPath.row;
UIImage *currentImage = [_imageList objectAtIndex:indexPath.row];
pictureView.image = currentImage;
return cell;

}

 @end
2

There are 2 best solutions below

0
On BEST ANSWER

Fixed it by adding the images as sub views through the code instead of the story board. No idea why it works, but it did.

1
On

i think u might missed the setting the layout for your collection view set it and try

- (void)viewDidLoad 
 {
   [super viewDidLoad];

   //...other code
   _imageList = [[NSMutableArray alloc] init];

   //set layout hear
   UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; //default layout
   [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
   flowLayout.minimumInteritemSpacing = 0.0f;
   flowLayout.minimumLineSpacing      = 0.33f; //set the offset between items
  _collectionView.showsHorizontalScrollIndicator = NO;
  _collectionView.showsVerticalScrollIndicator   = NO;
  [_collectionView setCollectionViewLayout:flowLayout];

  _collectionView.delegate = self;
  _collectionView.dataSource = self;
  [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"PictureCell"];
} 

 //implement this delegate method to return item size for each cell 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{
    return CGSizeMake(200.0f,300.0f);
}