UIButton Image not changing/updating

Go To StackoverFlow.com

3

First off I am still new to objective-c and still trying to learn as much as I can so please bear with me.

Right now I have a UIButton that I've created programmatically. When the button is pressed an UIActionSheet is brought up with the choice of "Camera," "Choose Photo" or "Cancel." The button image is then suppose to change to the image taken (if camera was chosen) or image picked (if the camera roll was chosen).

My problem is the button's image is not changing after UIImagePicker is dismissed. At first the button was created as a subView of a tableView and when I scrolled the tableView, it refreshed the button and the image changed (this is not how I wanted it to behave of course). However if the user decides they want to change or remove the image, they just push the button again and the UIActionSheet displays a third choice to "Remove Photo." After scrolling the tableView I realized the button image was not changing at all, but instead a new button was created on top of the old button so I move the UIButton code to viewDidLoad instead (based upon some information found here on stackoverflow). I have also removed the ability for the tableView to scroll as I did not need scrolling for it.

I've been trying to solve this problem for a few days now how to refresh either the button, or the view properly after either UIImagePicker is dismissed or UIActionSheet is dismissed. I have tried using setNeedsLayout and setNeedsDisplay but these have not worked to fix my problem (I might be putting these in the wrong place). Hopefully someone has insight on this and can guide me on the proper way to make this work. Also provided is my code:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Creates camera button and connects to camera menu UIActionSheet
    UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cameraButton addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
    cameraButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    cameraButton.titleLabel.textAlignment = UITextAlignmentCenter;
    cameraButton.frame = CGRectMake(9, 230, 80, 80);
    [self.view addSubview:cameraButton];

    picturePresent = NO;
    NSLog(@"picturePresent = %@", picturePresent);
}

-(void)showActionSheet:(id)sender {
    if (picturePresent == NO) {
    UIActionSheet *cameraMenuSheet = [[UIActionSheet alloc] initWithTitle:@"Add Photo" 
                                                                 delegate:self 
                                                        cancelButtonTitle:@"Cancel" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:@"Camera", @"Choose Photo", nil];
    [cameraMenuSheet showFromTabBar:self.tabBarController.tabBar];
    }
    else if (picturePresent == YES) {
    UIActionSheet *cameraMenuSheet = [[UIActionSheet alloc] initWithTitle:@"Add Photo" 
                                                                 delegate:self 
                                                        cancelButtonTitle:@"Cancel" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:@"Remove Photo", @"Camera", @"Choose Photo", nil];
    [cameraMenuSheet showFromTabBar:self.tabBarController.tabBar];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];

    // Initialize UIImagePickerController
    if ([title isEqualToString:@"Camera"]) {
        // Camera
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.showsCameraControls = YES;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];

        [self presentModalViewController:imagePicker animated:YES];
        newMedia = YES;
    }
    else if ([title isEqualToString:@"Choose Photo"]) {
        // Photo library
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
        [self presentModalViewController:imagePicker animated:YES];
        newMedia = NO;
    }
    else if ([title isEqualToString:@"Remove Photo"]) {
    picturePresent = NO;

    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self.view setNeedsDisplay];
    }

// Method for saving image to photo album
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    UIImage *scaledImage = [image scaleToSize:CGSizeMake(80.0, 80.0)];

        if (newMedia == YES) {
        // Save image
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

        }

        resizedImage = scaledImage;
    }

    picturePresent = YES;
    [self dismissModalViewControllerAnimated:NO];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissModalViewControllerAnimated:YES];
}

Additionally, now that I have moved my UIButton coding to viewDidLoad I am unsure of where to put this piece of coding:

{
    if (picturePresent == NO) {

        [cameraButton setTitle:@"Add\nPhoto" forState:UIControlStateNormal];
    }

    else if (picturePresent == YES) {

        [cameraButton setImage:resizedImage forState:UIControlStateNormal];
    }
}

Also setImage and setBackgroundImage were both used and still not working how I want it to. Let me know if more information is needed, thanks!

EDIT:

Here are some screenshots to show what I am aiming for -

screen1

screen2

screen3

"Picture" is suppose to represent a picture that was taken or selected and then scaled to fit the button size.

2012-04-04 21:05
by fresh


1

Did not test it, but if you make your cameraButton member of your UiViewController so that you can access whenever needed, you could call the setImage: forState: right after caculating the resizedImage - no picturePresent bool needed. And I think setNeedsDisplay Not needed as well.

EDIT:

In your view controller header file do something like

...
@interface MyViewController : UIViewController
{
   ...
   UIButton * cameraButton;
   ...
}
... 

Somewhere in your implementation file (viewDidiLoad is Ok) do:

...
cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
...    
[self.view addSubview:cameraButton];
...

And then implement all the other stuff you did and change the code in didFinishPickingMediaWithInfo like this:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{

   ...
  [cameraButton  scaledImage forState:UIControlStateNormal];
}

As I said, didn't test it, but I think it should work.

2012-04-04 21:27
by Kai Huppmann
Can you explain further what you mean by make it a member of UIViewController. Sorry I am still new to iOS and obj-c programming - fresh 2012-04-04 21:44
I will tomorrow. Working on phone at the moment - Kai Huppmann 2012-04-04 22:19
After a week of trying to wrap my head around this problem your code did the trick. Thanks for such s a simple, but great solution - fresh 2012-04-05 22:37


1

Move the viewDidLoad code to the end of imagePickerController:didFinishPickingMediaWithInfo:. Also if you can post a few screenshots it will help explain exactly what is going on, as well as what you expect. This could be a cropping issue, which is why I suggest posting screenshots.

2012-04-04 21:10
by Sam
I tried moving the button code to imagePickerController:didFinishPickingMediaWithInfo: and the button does not draw when loading this screen so I have moved it back to its original spot - fresh 2012-04-04 21:41


0

long time i've searched for answers..now here some:

to change image of a button the code below helped me:

[button.imageView setHighlighted:YES];
[button.imageView setHighlightedImage:[UIImage imageNamed:@"mypic.png"]];
[button.imageView setImage:[UIImage imageNamed:@"mypic.png"]];

..e.g. in viewDidLoad.

hope that helps...

2012-05-31 00:13
by kurtanamo
Ads