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

Showing two images in one ViewController picked with UIImagePickerController from different buttons

I created a ViewController (UIViewController) where I have two UIImageView (avatar and coverPhoto) and two UIButton. Clicking on the first button I call for an action sheet where I can choose between "taking photo from library" or "take a new image". It works.

When I click the second UIButton it has to do the same operation (calling for action sheet with two actions) but when I pick the image it replaces the first image.

I found the core of the problem in UIImagePickerController and I was trying with “tags” to identify which button invoke the action. But it doesn’t work.

Do you have any ideas how to implement it?

8 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hey Andrey Krylov,

It looks like you've implemented a similar solution to what I did solving your problem. Here's a video for you and those interested on how to do something like this.

I've used tags and some custom Enums to allow a user to change 2 UIImageViews in one view controller, using one action and multiple buttons.

For anyone who's interested in Using Tags and Enums in iOS you can download the project files here and follow along.

Regards,
Andrew

Wow, guys! Thank you for a video.. Really it is amazing! Andrew THANK YOU!

By the way I fixed that problem in another way.

header:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
// enum definition
typedef enum {
    CurrentImageCategoryAvatar = 0,
    CurrentImageCategoryCover
}CurrentImageCategory;

@interface HBProfileViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> // I didn't use here UIActionSheetDeelegate, but it works.
{

}

@property (weak, nonatomic) IBOutlet UIImageView *cover; // cover photo
@property (weak, nonatomic) IBOutlet UIImageView *avatar; // avatar photo
@property (weak, nonatomic) IBOutlet UILabel *usernameLabel; // outlet for displaying current username.

@property (nonatomic, weak) UIImagePickerController *imagePicker;
@property (nonatomic, weak) UIImage *image;

// I used two different actions for each button.
- (IBAction)changeAvatar:(id)sender;
- (IBAction)changeUserCover:(id)sender;

@end

and my .m-file:

#import "HBProfileViewController.h"
#import <Parse/Parse.h>

@interface HBProfileViewController ()
@end

@implementation HBProfileViewController

@synthesize cover, avatar, usernameLabel, imagePicker, image;

- (void)viewDidLoad
{
    [super viewDidLoad];
    PFUser *currentUser = [PFUser currentUser];
    if (currentUser) {
    self.usernameLabel.text = currentUser.username;
    }    
}

#pragma mark - Button to change user's avatar

// code "change avatar button" is below
- (IBAction)changeAvatar:(id)sender{

    [[NSUserDefaults standardUserDefaults] setInteger:CurrentImageCategoryAvatar forKey:@"currentImageCategory"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Set your avatar" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Choose existing photo", @"Take new photo", nil];
    [actionSheet showInView:self.view];
}

// actionSheet to "change avatar button"
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {

        NSLog(@"The %@ library button was tapped.", [actionSheet buttonTitleAtIndex:buttonIndex]);
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:NULL];

     }

    else if (buttonIndex == 1) {

        NSLog(@"The %@ camera button was tapped.", [actionSheet buttonTitleAtIndex:buttonIndex]);
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:NULL];

    }

}

# pragma mark - Button to change user's cover photo

//button to change user cover
- (IBAction)changeUserCover:(id)sender {

    [[NSUserDefaults standardUserDefaults] setInteger:CurrentImageCategoryCover forKey:@"currentImageCategory"];
    [[NSUserDefaults standardUserDefaults] synchronize];


    UIActionSheet *userCoverActionSheet = [[UIActionSheet alloc] initWithTitle:@"Set your cover photo" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Choose existing photo", @"Take new photo", nil];
    [userCoverActionSheet showInView:self.view];
}

// actionSheet to change user cover with the second picker
- (void)userCoverActionSheet:(UIActionSheet *)userCoverActionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 0) {


        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:NULL];

    }

    else if (buttonIndex == 1) {

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:NULL];
    }
}

# pragma mark - Image picker controller delegate methods

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    if([[NSUserDefaults standardUserDefaults] integerForKey:@"currentImageCategory"] == CurrentImageCategoryAvatar)
    {
        // Set image to avatar
        self.avatar.image = chosenImage;
    }
    else if([[NSUserDefaults standardUserDefaults] integerForKey:@"currentImageCategory"] == CurrentImageCategoryCover)
    {
        // Set image to cover
        self.cover.image = chosenImage;
    }




    [picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

@end

Nevertheless, Andrew and TH team thank you for a video. You code is much objective and cleaner than mine.

Andrey. PS. I will ask more))

Dear Andrey! Personally I haven't understood the problem. Could you share with us some more information regarding your issue?

Thanks in advance :)!

oh I am sorry I just mentioned that you replied. I have already fixed that problem.

The problem was with complexity to make "else if" condition inside UIImagePickerController. But I fixed it using typedef enum mwthod.

Even though I´m not coding IOS right now, i really like the reply.

+1 for TV Tutor Thursdays

This was an AMAZING demonstration to watch for someone still learning to code for iOS. Thank you so much, Andrew!

Guys from TreeHouse just inspire to learn code, just sit and bleed in front of computer!

Boy things have changed since I watched my first VB6 screencast back in the day and thought, hey! but isn't this a fun way to spend some the time while you are not coding. Thanks to the TH team for their outstanding support.

Hi can you provide a link to download the video? thanks!

Andrew Chalkley i used a uigesture recognizer but how can use tag with it