我创建了一个 ViewController,其中有两个图像头像和封面照片以及两个按钮。单击第一个按钮 (changeAvatar) 我调用了一个操作表,我可以在其中选择“从库中拍照”或“拍摄新图像”。第二个按钮的作用相同,但用于封面照片。
ProfileViewController.h
@interface ProfileViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *cover;
@property (strong, nonatomic) IBOutlet UIImageView *avatar;
- (IBAction)changeAvatar:(id)sender;
- (IBAction)changeUserCover:(id)sender;
在实现文件中:
@synthesize cover;
@synthesize avatar;
- (IBAction)changeAvatar:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Set your avatar" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Choose existing photo", @"Take new photo", nil];
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(@"The %@ library button was tapped.", [actionSheet buttonTitleAtIndex:buttonIndex]);
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
else if (buttonIndex == 1) {
NSLog(@"The %@ camera button was tapped.", [actionSheet buttonTitleAtIndex:buttonIndex]);
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
头像的图像选择器控制器:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.avatar.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
我对第二个按钮做了同样的事情,但是当我点击按钮时,选择的图像(coverPhoto)会转到头像图像。
我正在尝试使用“标签”来识别哪个按钮调用了该操作。但它不起作用。你有什么想法来实现它吗?