Can't Change Background of UIButton ios - ios

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

Try this,
[ButtonPicture setBackgroundImage:gotImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

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

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

Related

Removing the cancel button from Custom camera

I am using a custom overlay frame, on camera. On the navigation bar I have app icon in the middle and a back button on left.
So I would like to remove the default cancel button besides the capture button but I do not want to remove camera click button. I have done research but I did not find a perfect answer. I have already used the following code but it also removes the click button.
[picker setShowsCameraControls:YES];
I feel you you should add custom CameraOverlayView to your uiimagepickercontroller and hide CameraControls.This won’t show your cancel button.
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[picker setShowsCameraControls:NO];
CGRect screenBounds= [UIScreen mainScreen].bounds ;
UIView *overlayView = [[UIView alloc] initWithFrame:screenBounds];
[overlayView setAutoresizesSubviews:YES];
[overlayView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin]; UIButton *captureButton=[[UIButton alloc]initWithFrame:CGRectMake(screenBounds.size.width/2-30, screenBounds.size.height-60,60 , 60)];
[captureButton addTarget:self action:#selector(captureImage:) forControlEvents:UIControlEventTouchUpInside];
[captureButton setBackgroundImage:[UIImage imageNamed:#"icon"] forState:UIControlStateNormal];
[overlayView addSubview:captureButton];
[picker setCameraOverlayView:overlayView];
[picker setCameraDevice:UIImagePickerControllerCameraDeviceRear];
[self presentViewController:picker animated:YES completion:NULL];
And this is action for Capture button.
-(void)captureImage:(UIButton*)sender
{
[picker takePicture];
}
And use this delegate method to get image.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
I think you disable but about removing i am not sure as I know it is not possible because UIImagePickerController inherits UINavigationController. Then you can get
UINavigationBar *bar = picker.navigationBar;
[bar setHidden:NO];
bar.navigationItem.rightBarButtonItem = doneButton;
It will work only for IOS 6
You can use this source ELCImagePickerController

iOS how to start the image picker

I have a UIButton and when the user clicks it, I want to start the image picker controller
I already configure what I think it is enough but I couldn't know what is the message that I have to pass to that image picker to start working
This is my code
#import "ImagePickerViewController.h"
#interface ImagePickerViewController ()
#property (weak, nonatomic) IBOutlet UIImageView *image;
#end
#implementation ImagePickerViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)SelectImage:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
#end
I have already set the correct protocols:
#interface ImagePickerViewController : UIViewController < UINavigationControllerDelegate,UIImagePickerControllerDelegate>
#end
You need to present the UIImagePicker
- (IBAction)SelectImage:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:NO completion:nil];
}
You can show the picker with:
[self presentViewController:imagePicker animated:YES completion:NULL];
And then in your delegate callback imagePickerController:didFinishPickingMediaWithInfo you get the image and dismiss the picker again:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
You have to tell your view controller class to show the UIImagePickerController.
Simply use
[self presentViewController:imagePicker animated:YES completion:nil];
This will present it modally. If you are running on an iPad, you instead have to present it in a popover controller, as follows:
Declare a new property in the #interface:
#property (nonatomic, strong) UIPopoverController* imagePickerPopover;
Present the popover controller e.g:
UIButton* button = sender;
self.imagePickerPopover = [[UIPopoverController alloc] initWithContentViewController:self.imagePicker];
[self.imagePickerPopover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];

Hide ActionSheet iOS

My application is about clicking on a button with background image named "uploadphoto1.png". When I click on the button, it shows me an ActionSheet letting me choose to take a picture with the camera or taking a picture from the library.
When, for example, I choose a picture from the camera, it lets me select a picture and show the picture in an imageView and it change the background image of the button from upload.png to save.png.
The problem now that when I click on the button with background Image "save1.png" it shows me the actionSheet.
I want to hide the actionSheet.
Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *buttonpicture =[UIImage imageNamed:#"uploadphoto1.png"];
[ButtonPicture setBackgroundImage:buttonpicture forState:UIControlStateNormal];
[ButtonPicture setFrame:CGRectMake(173, 269, 140, 30)];
}
- (IBAction)ButtonPicture:(UIButton *)sender
{
NSLog(#"Button Picture Pressed");
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;
default:
break; }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[ButtonPicture setBackgroundImage:[UIImage imageNamed:#"save1.png"] forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
imageview.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//[actionSheet showInView:Nil];
}
#interface YourViewController
{
BOOL imageChanged;
}
- (void)viewDidLoad
{
[super viewDidLoad];
imageChanged = NO;
UIImage *buttonpicture =[UIImage imageNamed:#"uploadphoto1.png"];
[ButtonPicture setBackgroundImage:buttonpicture forState:UIControlStateNormal];
[ButtonPicture setFrame:CGRectMake(173, 269, 140, 30)];
}
- (IBAction)ButtonPicture:(UIButton *)sender
{
NSLog(#"Button Picture Pressed");
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:#"Photo" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Picture From Camera",#"Picture From Gallery",nil];
[actionSheet setTag:1001];
if (!imageChanged)
[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;
default:
break;
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[ButtonPicture setBackgroundImage:[UIImage imageNamed:#"save1.png"] forState:UIControlStateNormal];
imageChanged = YES;
[picker dismissModalViewControllerAnimated:YES];
imageview.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
There are various ways this could be handled. One way is to set a boolean flag when you don't want tapping on the button to bring up the actionSheet. Then, in - (IBAction)ButtonPicture:(UIButton *)sender you can check the boolean flag to decide whether or not to create the actionSheet and show it.

Pass image from camera to view from storyboard

Something strange is happening with my code.
Scenario: Open camera->Take picture->Pass the image taken to a viewcontroller(from a storyboard.
The thing is that my UIimage variable from the destination view is not available, not recognized!
My code
postviewController.h
#import <UIKit/UIKit.h>
#interface postAlertViewController : UIViewController
#property (nonatomic, retain) UIImage *cameraImage;
#end
postviewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *aImageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width,150 )];
aImageview.image=cameraImage;
[self.view addSubview:amageview];
}
Take picture and pass it to the view controller above
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
ViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
postView.cameraImage=(UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
[self.navigationController pushViewController:postView animated:YES];
}
The stroryboard ID is set to "postview".
What i am getting is:
Property 'cameraImage' not found on object of type 'ViewController *'
Why cameraImage is not available from the origin view although is declared in destination's .h file?
In another situation where i needed to pass a string between views, in same manner as above
[yourViewController setValue:mysc.title forKey:#"sendAlertTitle"];
worked ok.
Any help?
Thank you.
try this
postAlertViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
and always use class and interface name start with capital
you can use this one for more ease.
- (IBAction)captureImage:(id)sender
{
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:#"No Device" message:#"Camera is not available"
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[deviceNotFoundAlert show];
} else {
UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.delegate =self;
// Show image picker
[self presentViewController:cameraPicker animated:YES completion:nil];
}
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage *selectedImage =[info objectForKey:UIImagePickerControllerOriginalImage];
ViewControllerName *Vc=[self.storyboard instantiateViewControllerWithIdentifier:#"captioEdit"];
Vc.postImgStr=[self scaleAndRotateImage:selectedImage];
[picker dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:captioViewEdit animated:YES];
}
//Delegate method of UIImagePickerController for image picker model cancel
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
Thanks

Passing an image from one view to other iOS

I have 2 UIViewControllers in my app.
In the 1st viewController, i have a UIButton in a toolbar.
When i click on the button, it displays me a UIActionSheet to select whether to take a picture from camera or from gallery.
When i choose, for example, taking picture from gallery and i choose a picture, it displays the picture in the 1st viewController.
What i want is to take a picture from gallery photos or from camera and to go to the 2nd viewController and display the image in the 2nd viewController.
I searched a lot and tried so much codes but it doesn't work.
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 {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
In my second view i created a UIImageView here is my code in SecondViewController.h
#interface SecondViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *imageButton;
#end
Can You help me please.
Sorry for my bad English and thanks
It seems you want to select image at FirstViewController and display it in SecondViewContoller. However, you did not do anything to switch to SecondViewController and pass your selected image to it in ViewController.m.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.imageButton.image = image;
[self.navigationController pushViewController:secondViewController animated:YES];
}

Resources