如何区分didFinishPickingMediaWithInfo中的UIImagePickerController

Posted

技术标签:

【中文标题】如何区分didFinishPickingMediaWithInfo中的UIImagePickerController【英文标题】:How to distuingish between UIImagePickerController in didFinishPickingMediaWithInfo 【发布时间】:2015-10-28 10:58:23 【问题描述】:

我有两个UIImageViews,我想在其中加载两个不同的图像。我有两个按钮触发UIImagePickerController 并调用我的

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

方法。

有没有办法区分上面的方法调用的是哪个picker?

【问题讨论】:

是否有任何按钮标题,如果是,则在按钮单击时将按钮标题存储在字符串中,如果 -didFinishPickingMediaWithInfo 中的其他条件与按钮标题一起使用,您可以执行此操作? 不,我使用的是图形,而不是标题字符串。是的,它的图形相同;) ok @piyushsharma 的回答是对的..+1 【参考方案1】:

你可以创建一个 UIButton 的实例

@interface YourClass ()

   UIButton *_selectedButton;

在触发UIImagePickerController之前,可以将按钮保存在实例变量中

-(void)button1Clicked:(UIButton *)button1
      _selectedButton = button1;
     // call UIImagePickerController
 

-(void)button2Clicked:(UIButton *)button2
      _selectedButton = button2;
      // call UIImagePickerController
 


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info   
      if (_selectedButton == button1)
         // perform your logic 
       else if (_selectedButton == button2)
        // perform your logic 
      

【讨论】:

酷!解决了任何问题!谢谢,这真的很聪明!【参考方案2】:

因为我只是在尝试同样的问题,所以我也会发布我的答案。我没有使用单独的UIButtonUIImageView 提供点击事件。这是我的完整代码 -

#import "ViewController.h"

#define IMAGE1 1
#define IMAGE2 2

@interface ViewController ()<UINavigationControllerDelegate ,UIImagePickerControllerDelegate,  UIGestureRecognizerDelegate, UIActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *image1;
@property (weak, nonatomic) IBOutlet UIImageView *image2;
@property NSInteger selectedImage;

@end

@implementation ViewController

- (void)viewDidLoad 
    [super viewDidLoad];
    [self initImageView];
    // Do any additional setup after loading the view, typically from a nib.


- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


- (void)initImageView 

    _image2.userInteractionEnabled = YES;
    _image1.userInteractionEnabled = YES;

    UITapGestureRecognizer *image1Gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPhoto:)];
    image1Gesture.numberOfTapsRequired = 1;
    image1Gesture.numberOfTouchesRequired = 1;
    [image1Gesture setDelegate:self];
    _image1.tag = IMAGE1;
    [_image1 addGestureRecognizer:image1Gesture];


    UITapGestureRecognizer *image2Gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPhoto:)];
    image2Gesture.numberOfTapsRequired = 1;
    image2Gesture.numberOfTouchesRequired = 1;
    [image2Gesture setDelegate:self];
    _image2.tag = IMAGE2;
    [_image2 addGestureRecognizer:image2Gesture];



-(void)addPhoto:(UIGestureRecognizer*) gestureRecognizer

    NSLog(@"Tag = %ld",(long)[gestureRecognizer view].tag);

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Select Source"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:@"Camera",
                            @"Gallery",nil];
    // Show the sheet
    if([gestureRecognizer view].tag == IMAGE1) 
        sheet.tag = IMAGE1;
     else if([gestureRecognizer view].tag == IMAGE2) 
        sheet.tag = IMAGE2;
     else 
        sheet.tag = 000;
    

    [sheet showInView: self.view];


#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 

    _selectedImage = actionSheet.tag;
    switch (buttonIndex) 
        case 0:
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
                [self performSelector:@selector(takePhoto) withObject:nil afterDelay:0.3];
             else 
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"The device have no camera" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];

            
            break;

        case 1:
            [self performSelector:@selector(selectPhoto) withObject:nil afterDelay:0.3];
            break;

        case 2:
            NSLog(@"Invalid option");
            break;
    



// take photo from camera
- (void)takePhoto 

    NSLog(@"takePhoto from camera");
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
    [self presentViewController:imagePicker animated:NO completion:nil];



// select photo from gallery
- (void)selectPhoto 

    NSLog(@"SelectPhoto from gallery");
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePicker animated:NO completion:nil];



#pragma mark - UIImagePickerControllerDelegate delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:nil];

    if (_selectedImage == IMAGE1) 
        _image1.image = chosenImage;
     else if (_selectedImage == IMAGE2) 
        _image2.image = chosenImage;
     else 
        NSLog(@"Could not determine image selection");
    


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
    return 1;


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
    return 1;


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
    [picker dismissViewControllerAnimated:NO completion:nil];

@end

【讨论】:

以上是关于如何区分didFinishPickingMediaWithInfo中的UIImagePickerController的主要内容,如果未能解决你的问题,请参考以下文章

如何查看mysql表名字段是不是区分大小写

如何区分对联

MySQL如何区分大小写

如何设置MySQL中表的大小写区分

Win10各版本如何区分

Oracle中如何去除大小写区分的设置?!