0

我创建了一个 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)会转到头像图像。

我正在尝试使用“标签”来识别哪个按钮调用了该操作。但它不起作用。你有什么想法来实现它吗?

4

1 回答 1

2

使用枚举类型CurrentImageCategory

typedef enum {
CurrentImageCategoryAvatar = 0,
CurrentImageCategoryCover
}CurrentImageCategory;

用于userDefaults存储当前图像类别,或者您可以在班级中管理自己的财产。

- (IBAction)changeAvatar:(id)sender{
  // Set current category to userDefaults
  [[NSUserDefaults standardUserDefaults] setInteger:CurrentImageCategoryAvatar forKey:@"currentImageCategory"];
  [[NSUserDefaults standardUserDefaults] synchronize];

  // Your code
}

使用相同的封面图像,

- (IBAction)changeUserCover:(id)sender{
  // Set current category to userDefaults
  [[NSUserDefaults standardUserDefaults] setInteger:CurrentImageCategoryCover forKey:@"currentImageCategory"];
  [[NSUserDefaults standardUserDefaults] synchronize];

  // Your code
}

然后在imagePickerController检查currentImageCategory

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    if([[NSUserDefaults standardUserDefaults] integerForKey:@"currentImageCategory"] == CurrentImageCategoryAvatar)
    {
        // Set image to avatar
    }
    else if([[NSUserDefaults standardUserDefaults] integerForKey:@"currentImageCategory"] == CurrentImageCategoryCover)
    {
       // Set image to cover
    }
}
于 2013-11-12T03:43:13.487 回答