XCode UIActionSheet图像

时间:2014-07-24 05:07:08

标签: ios xcode uiimagepickercontroller uiactionsheet

我正在创建UIButton,当用户点击它时,相机应弹出并允许用户拍照。我决定添加一个UIActionSheet,允许用户选择他们想要从相册中选择一张照片,或者他们是否想要使用相机拍照。在我添加操作表之前,按钮工作正常,当用户塑造照片时,按钮的背景图像被UIImagePicker拍摄的照片替换。但是,在我添加了操作表后,UIButtons图像没有被从UIImagePicker或用户照​​片库中拍摄的图像替换

这是关联的代码,

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

1 个答案:

答案 0 :(得分:0)

您无需再创建另一个按钮

只需要在接口中声明uploadPhotoButton以便在此控制器中全局使用,只需替换下面的方法:

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

在这段代码中,我首先想念图像pickerViewController,然后在我的按钮上设置图像。