如何用从ImagePickerController拍摄的照片替换UIButton图像



我创建了一个按钮,当按下它时,会弹出相机。用户应该拍一张照片,然后这张照片应该取代按钮的图像。但是,相机拍摄了照片,当它被驳回时,按钮的图像保持不变,以下是我的代码片段:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.usernameField becomeFirstResponder];
    UIButton *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)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    UIImage *userImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [uploadPhotoButton setImage:userImage forState:UIControlStateNormal];
    [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
}

你知道问题在哪里吗?

是否在

中声明了uploadPhotoButton
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

中的uploadPhotoButton相同
- (void)viewDidLoad

?

也许不是!

最新更新