如何用从 ImagePickerController 拍摄的照片替换 UIButton 图像
Posted
技术标签:
【中文标题】如何用从 ImagePickerController 拍摄的照片替换 UIButton 图像【英文标题】:How to replace a UIButton image with a photo taken from ImagePickerController 【发布时间】:2014-07-21 00:20:09 【问题描述】:我创建了一个按钮,按下该按钮会调出相机。用户应该拍照,然后这张照片应该替换按钮的图像。但是,相机拍摄照片,当它被关闭时,按钮的图像保持不变,这是我的代码 sn-p:
- (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];
知道问题出在哪里吗?
【问题讨论】:
您的问题不是按钮图像而是按钮本身。这段代码甚至无法编译。 @Desdenova 代码编译运行,按钮调出摄像头,唯一的就是按钮上没有替换图片。 【参考方案1】:是在
中声明的uploadPhotoButton-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
与uploadPhotoButton中的一样
- (void)viewDidLoad
?
也许不是!
【讨论】:
以上是关于如何用从 ImagePickerController 拍摄的照片替换 UIButton 图像的主要内容,如果未能解决你的问题,请参考以下文章