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 Build a Self-Destructing Message iPhone App Capturing Photo and Video Using UIImagePickerController Adding Recipients

charlie D
charlie D
5,218 Points

Build a Self-Destructing Message iPhone App/Adding Recipients

I've gone through my code a few times but I can't figure out why I'm have this issue. I'm using XCODE 6.1

Issue: When I click on "Camera" select photo, my Recipients/list of friends will appear for 2 seconds, then my camera/photos view reappears when my RecipientsViewController should remain in order for me to select friends to send photo to.

I'm using the following code for my CameraViewController.m:

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


@interface CameraViewController ()

@end

@implementation CameraViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];

}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    PFQuery *query = [self.friendsRelation query];
    [query orderByAscending:@"username"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
        else {
            self.friends = objects;
            [self.tableView reloadData];
        }
    }];

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



    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];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.friends count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFUser *user = [self.friends objectAtIndex:indexPath.row];
    cell.textLabel.text = user.username;

    return cell;
}


#pragma mark - Image Picker Controller 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 or selected!
        self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            //Save the image!
            UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
        }

    }
    else {
        //A video was selected
        NSURL *imagePickerURL = [info objectForKey:UIImagePickerControllerMediaURL];
        self.videoFilePath = [imagePickerURL path];    }
    if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        //Save the video!
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.videoFilePath))
        UISaveVideoAtPathToSavedPhotosAlbum (self.videoFilePath, nil, nil, nil);
    }
     [self dismissViewControllerAnimated:YES completion:nil];

}


@end

Any suggestions?

2 Answers

Kevin Hamil
Kevin Hamil
8,203 Points

I've come across several inconsistencies (just a few) throughout the iOS track when working with xcode 6+.. and this was one of them. I honestly spent some time trying to figure it out, but I never did and, ultimately, I downloaded the project files (which I highly recommend when needing to sort out an issue like this - it's easy to forget it's available).

There was an if statement that wrapped the imagePicker code in viewWillAppear. I'm fairly new to Obj-C, so I can't explain what the original problem was or specifically why this fixed it, but it did and I proceeded through the course.

Note: The if statement is added in during a later video in course.

if (self.image == nil && [self.videoFilePath length] == 0) {
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = NO;
    self.imagePicker.videoMaximumDuration = 10;

    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];
}
Benjamin McMahan
Benjamin McMahan
6,679 Points

Thank you! This fixed it for me, running Xcode 6.1.1. Was beginning to think I would not be able to make it to the next step. A note about this under Teachers Notes is needed.

charlie D
charlie D
5,218 Points

Ok, thanks Kevin! I'll look out for that "IF" statement. Yeah, I had another issue that I was able to resolve by going to the app developer website. With this specific issue, I upgraded my XCODE from 6 to 6.1 - thinking there may be a bug - but it didn't resolve the issue.

It seems like I may need to add an additional line of code somewhere - due to Xcode 6 - but i can't figure it out because I'm not getting any error messages. When I first noticed the issue, i redid that particular section, looked at project file, and there code appeared to be identical to mine. I'll keep searching for a solution.

Thanks...

ever find a solution? same ish here