Ios XCode UIActionSheet图像
我正在创建一个Ios XCode UIActionSheet图像,ios,xcode,uiimagepickercontroller,uiactionsheet,Ios,Xcode,Uiimagepickercontroller,Uiactionsheet,我正在创建一个ui按钮,当用户单击它时,相机应该会弹出并允许用户拍照。我决定添加一个UIActionSheet,它允许用户选择是从相册中选择照片,还是使用相机拍照。在我添加操作表之前,按钮工作正常,当用户塑造照片时,按钮的背景图像被UIImagePicker拍摄的照片所取代。但是在我添加了操作表之后,ui按钮图像并没有被从ui图像采集器或用户照片库中拍摄的图像所取代 这是相关代码 - (void)viewWillAppear:(BOOL)animated { [super viewWillApp
ui按钮
,当用户单击它时,相机应该会弹出并允许用户拍照。我决定添加一个UIActionSheet
,它允许用户选择是从相册中选择照片,还是使用相机拍照。在我添加操作表之前,按钮工作正常,当用户塑造照片时,按钮的背景图像被UIImagePicker
拍摄的照片所取代。但是在我添加了操作表之后,ui按钮
图像并没有被从ui图像采集器
或用户照片库中拍摄的图像所取代
这是相关代码
- (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];
-(void)视图将显示:(BOOL)动画{
[超级视图将显示:动画];
self.navigationController.navigationBarHidden=是;
[self.navigationItem setHidesBackButton:否];
[self.usernameField成为第一响应者];
uploadPhotoButton=[UIButton按钮类型:UIButtonTypeCustom];
[uploadPhotoButton setImage:[UIImage ImageName:@“uploadphotoicon”]用于状态:UIControlStateNormal];
uploadPhotoButton.frame=CGRectMake(0,0,80,80);
uploadPhotoButton.clipsToBounds=是;
上传PhotoButton.layer.cornerRadius=80/2.0f;
uploadPhotoButton.layer.borderColor=[UIColor COLOR WITHRED:.102绿色:.737蓝色:.612 alpha:1.0]。CGColor;
上传PhotoButton.layer.borderWidth=2.0f;
[self.view addSubview:uploadPhotoButton];
[上传PhotoButton设置中心:CGPointMake(160128)];
[uploadPhotoButton添加目标:自我操作:@选择器(uploadPhotoButton:)用于控制事件:UIControlEventTouchUpInside];
-(无效)操作表:(UIActionSheet*)操作表单击按钮索引:(NSInteger)按钮索引{
如果(按钮索引<2){
UIImagePickerController*imagePicker=[[UIImagePickerController alloc]init];
UIImagePickerController资源类型;
开关(按钮索引){
案例0:
类型=UIImagePickerControllerSourceTypePhotoLibrary;
打破
案例1:
类型=UIImagePickerControllerSourceTypeCamera;
打破
违约:
打破
}
imagePicker.sourceType=类型;
imagePicker.delegate=self;
[self-presentViewController:imagePicker动画:是完成:^{}];
}
}
-(iAction)注册完成:(id)发件人{
NSString*username=[self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet-whitespaceAndNewlineCharacterSet]];
NSString*password=[self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet-whitespaceAndNewlineCharacterSet]];
如果([username length]==0 | |[password length]==0/*| |[email length]==0*/){
UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@“Oops!”消息:@“请输入用户名、密码和电子邮件!”代理:nil cancelButtonTitle:@“确定”其他按钮:nil,nil];
[警报视图显示];
}
否则{
PFUser*newUser=[PFUser];
newUser.username=用户名;
newUser.password=密码;
[newUser signupinbackgroundithblock:^(BOOL成功,NSError*错误){
如果(错误){
UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@“对不起”消息:[error.userInfo objectForKey:@“error”]委托:nil cancelButtonTitle:@“确定”其他ButtonTitles:nil,nil];
[警报视图显示];
}
否则{
[self.navigationController-popToRootViewControllerAnimated:是];
}
}];
}
}
-(iAction)上传照片按钮:(id)发送者{
UIActionSheet*sheet=[[UIActionSheet alloc]initWithTitle:@“选择照片”委托:自取消按钮:@“无终止”销毁按钮:无其他按钮:@“来自库”,“来自相机”,无];
[图纸显示视图:self.view];
}
-(void)imagePickerController:(UIImagePickerController*)picker未使用信息完成PickingMediaWithInfo:(NSDictionary*)信息{
uploadPhotoButton=[UIButton按钮类型:UIButtonTypeCustom];
uploadPhotoButton.frame=CGRectMake(0,0,80,80);
uploadPhotoButton.clipsToBounds=是;
上传PhotoButton.layer.cornerRadius=80/2.0f;
uploadPhotoButton.layer.borderColor=[UIColor COLOR WITHRED:.102绿色:.737蓝色:.612 alpha:1.0]。CGColor;
上传PhotoButton.layer.borderWidth=2.0f;
[self.view addSubview:uploadPhotoButton];
[上传PhotoButton设置中心:CGPointMake(160128)];
[uploadPhotoButton添加目标:自我操作:@选择器(uploadPhotoButton:)用于控制事件:UIControlEventTouchUpInside];
userImage=[info objectForKey:UIImagePickerControllerEditedImage];
[上载PhotoButton setImage:userImage for状态:UIControlStateNormal];
[self.navigationController dismissViewControllerAnimated:是完成:NULL];
[自上传图像];
您不需要再创建一个按钮
您只需在界面中声明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];
}
在这段代码中,我首先忽略了image pickerViewController,只是在我的按钮上设置了图像