在一个ViewController中显示两个图像,从不同的按钮中选择UIImagePickerController

时间:2013-11-12 02:24:19

标签: ios objective-c image uiimagepickercontroller

我创建了一个ViewController,其中我有两个图像头像和coverPhoto以及两个按钮。点击第一个按钮(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)转到头像图像。

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

1 个答案:

答案 0 :(得分:2)

使用枚举类型CurrentImageCategory as,

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
    }
}