UICollectionView不显示来自CameraImage的图像 - 目标C.

时间:2015-06-10 13:56:24

标签: objective-c camera uiimageview uicollectionview

搜索论坛,找不到合适的答案。我怀疑代码有问题。我正在用相机拍照并试图在UICollectionView中显示它们。 collectionView里面有一个带有UIImageView的单元格。 即使设置静态图像和背景颜色,我仍然看不到任何东西(甚至没有颜色)。 代码如下:

 #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 个答案:

答案 0 :(得分:0)

我想您可能会错过设置集合视图的布局设置并尝试

- (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);
}

答案 1 :(得分:0)

通过代码而不是故事板将图像添加为子视图来修复它。不知道它为什么会起作用,但确实如此。