Search code examples
iosxcodeuiimagepickercontrolleruiactionsheet

XCode UIActionSheet image


I am creating a UIButton and when the user clicks on it, the camera should pop up and allow the user to take a photo. I decided to add a UIActionSheet which allows the user to choose wether they want to choose a photo from the album, or whether they want to take a photo using the camera. Before I added the action sheet the button worked fine, when the user shaped the photo, the background image of the button was replaced by the photo taken by the UIImagePicker. But after I added the action sheet now the UIButtons image is not being replaced by the images taken from the UIImagePicker or from the users photo library

here is the associated code,

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
[self.navigationItem setHidesBackButton:NO];

[self.usernameField becomeFirstResponder];
uploadPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];

[uploadPhotoButton setImage:[UIImage imageNamed:@"uploadphotoicon"] forState:UIControlStateNormal];

uploadPhotoButton.frame = CGRectMake(0, 0, 80, 80);

uploadPhotoButton.clipsToBounds = YES;

uploadPhotoButton.layer.cornerRadius = 80/2.0f;
uploadPhotoButton.layer.borderColor = [UIColor colorWithRed:.102 green:.737 blue:.612 alpha:1.0].CGColor;
uploadPhotoButton.layer.borderWidth = 2.0f;

[self.view addSubview:uploadPhotoButton];
[uploadPhotoButton setCenter:CGPointMake(160, 128)];
[uploadPhotoButton addTarget:self action:@selector(uploadPhotoButton:) forControlEvents:UIControlEventTouchUpInside];






 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex < 2) {
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    UIImagePickerControllerSourceType type;

    switch (buttonIndex) {
        case 0:
            type = UIImagePickerControllerSourceTypePhotoLibrary;
            break;
        case 1:
            type = UIImagePickerControllerSourceTypeCamera;
            break;

        default:
            break;
    }

    imagePicker.sourceType = type;
    imagePicker.delegate = self;

    [self presentViewController:imagePicker animated:YES completion:^{ }];
}

 }
 - (IBAction)signupDone:(id)sender {
     NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([username length] == 0 || [password length] == 0 /*|| [email length] == 0*/){
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Please enter a username, password, and email!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
     [alertView show];
}
else {
    PFUser *newUser = [PFUser user];
    newUser.username = username;
    newUser.password = password;

    [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alertView show];
        }
        else {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];

  }



  }
  -(IBAction)uploadPhotoButton:(id)sender {

UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Select Photo" delegate:self cancelButtonTitle:@"Nevermind" destructiveButtonTitle:nil otherButtonTitles:@"From Library", @"From Camera", nil];
[sheet showInView:self.view];



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


uploadPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];


uploadPhotoButton.frame = CGRectMake(0, 0, 80, 80);

uploadPhotoButton.clipsToBounds = YES;

uploadPhotoButton.layer.cornerRadius = 80/2.0f;
uploadPhotoButton.layer.borderColor = [UIColor colorWithRed:.102 green:.737 blue:.612 alpha:1.0].CGColor;
uploadPhotoButton.layer.borderWidth = 2.0f;

[self.view addSubview:uploadPhotoButton];
[uploadPhotoButton setCenter:CGPointMake(160, 128)];
[uploadPhotoButton addTarget:self action:@selector(uploadPhotoButton:) forControlEvents:UIControlEventTouchUpInside];


userImage = [info objectForKey:UIImagePickerControllerEditedImage];
[uploadPhotoButton setImage:userImage  forState:UIControlStateNormal];
[self.navigationController dismissViewControllerAnimated:YES completion:NULL];



[self uploadImage];

Solution

  • You do not need to create another button,

    Only you need to declare uploadPhotoButton in interface for global use in this controller and just replace the method below :

    - (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
        [picker dismissViewControllerAnimated:YES completion:NULL];      
        UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
    
        [uploadPhotoButton setImage:chosenImage  forState:UIControlStateNormal];
    //    [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
    
        [self uploadImage];
    }
    

    In this code I first diss miss the image pickerViewController and just set the image on my button.