Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Presenting ImagePicker modally over a CollectionViewController

Having problems displaying my imagePicker over the collectionviewController, any ideas?

If i change viewWillAppear to viewDidAppear i can see it but this is obviously not what I'm after.

#import "CameraViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
#import "HomeViewController.h"
#import "PhotoCell.h"

@interface CameraViewController ()

@end

@implementation CameraViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    CameraViewController *vc = (CameraViewController *) [storyBoard instantiateViewControllerWithIdentifier:@"Camera"];

    [vc initWithCoder:nil];
    self.collectionView.backgroundColor = [UIColor whiteColor];

    [self.collectionView registerClass:[PhotoCell class] forCellWithReuseIdentifier:@"camera"];

}

- (void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = NO;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    else {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }

    self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];

   [self presentViewController:self.imagePicker animated:NO completion:nil];

}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"camera";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];


    cell.backgroundColor = [UIColor lightGrayColor];

    return cell;
}

- (id)initWithCoder:(NSCoder*)coder
{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];



    layout.itemSize = CGSizeMake(306.0, 306.0);
    layout.minimumInteritemSpacing = 150.0;
    layout.minimumLineSpacing = 50.0;


    return (self = [super initWithCollectionViewLayout:layout]);
}



#pragma mark - ImagePickerController Delegate

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:NO completion:nil];

    [self.tabBarController setSelectedIndex:0];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        //a photo was taken/selected!
        self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            // save the image
            UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end

any ideas Ben Jakuben ? thanks!

3 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hmmm...I'm at a loss! Maybe Sam Soffes will have an idea.

Is there a particular reason you want the camera to show every time you load that view?

Also, I do not understand why you have this:

 UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    CameraViewController *vc = (CameraViewController *) [storyBoard instantiateViewControllerWithIdentifier:@"Camera"];

Couldn't you just add the class file in the storyboard?

 UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    CameraViewController *vc = (CameraViewController *) [storyBoard instantiateViewControllerWithIdentifier:@"Camera"];

Is that there a reason for this? Couldn't you just add the Class to the viewcontroller in the storyboard?

    I did that so that i could then do:
[vc initWithCoder:nil];

as I've customised the initWithCoder method.

I think because I only want to display one image i could just use a normal ViewController with an image View instead of the CollectionViewController to potentially avoid the problem