Reputation: 1
When a user clicks on UIButton that has background image that i put from storyboard , it will display a UIActionSheet letting the user to choose from the options:
1) Take a picture from camera
2) Picture from gallery photos.
If for example, he chooses to pick a picture from gallery photos, when he selects an image and returns to the view and display the image , i want to change the background image of the UIButton to another one that i want to put it programatically.
Here is the code of the ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
UIImageView * imageView;
UIToolbar *toolbar ;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *ButtonPicture;@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
@property (nonatomic, retain) IBOutlet UIImageView * imageView;
- (IBAction)PictureButton:(UIButton *)sender;
@end
Here is the code of ViewController.m
- (IBAction)PictureButton:(UIButton *)sender {
NSLog(@"Click button take picture in toolbar view 1");
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Photo"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Picture From Camera", @"Picture From Gallery", nil];
[actionSheet setTag:1001];
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex)
{
case 0: {
// Take a picture from camera
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
break;
case 1: {
// take a picture from gallery photos
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self.view addSubview:toolbar];
[self presentModalViewController:picker animated:YES];
}
break;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *gotImage =
[info objectForKey:UIImagePickerControllerOriginalImage];
[PictureButton setBackgroundImage:gotImage forState:UIControlStateNormal];
}
When i select an image from Library , i return to the view , i display the image that i selected but the UIBUtton Background won't change to another background. Thanks
Upvotes: 0
Views: 144
Reputation: 1
on tapping which button does your action sheet get called?is it the UIBarButtonItem *ButtonPicture? if yes then set the background image as [ButtonPicture setBackgroundImage:gotImage forState:UIControlStateNormal];
Upvotes: 0
Reputation: 17595
This is (IBAction)PictureButton:(UIButton *)sender;
method, you have wired up with your storyboard. As same as, you have to wired with UIBarbuttonItam property
as. ButtonPicture(not PictureButton)
. So you can try this.
-(void)changeImage:(UIImage*)newImage
{
UIImage *configImage = newImage;
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0,0, self.ButtonPicture.width, 32)];
[btn setBackgroundImage:configImage forState:UIControlStateNormal];
[btn addTarget:self.ButtonPicture.target action:self.ButtonPicture.action forControlEvents:UIControlEventTouchUpInside];
self.ButtonPicture.customView = btn;
}
call this method instead of [PictureButton setBackgroundImage:gotImage forState:UIControlStateNormal];
.
Upvotes: 0
Reputation: 1677
Try this,
[ButtonPicture setBackgroundImage:gotImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Upvotes: 1