如何用UIImagePickerController中的照片替换UIButton图像

时间:2014-07-21 00:20:10

标签: ios objective-c uibutton uiimage

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

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

知道问题出在哪里?

1 个答案:

答案 0 :(得分:0)

是否在

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

中的uploadPhotoButton相同
- (void)viewDidLoad

也许不是!